Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like search in Elasticsearch

Tags:

I am using elasticsearch for filtering and searching from json file and I am newbie in this technology. So I am little bit confused how to write like query in elasticsearch.

select * from table_name where 'field_name' like 'a%' 

This is mysql query. How do I write this query in Elasticsearch? I am using elasticsearch version 0.90.7.

like image 270
Dixit Sourav Avatar asked Jul 02 '15 05:07

Dixit Sourav


People also ask

What is wildcard search in Elasticsearch?

A wildcard operator is a placeholder that matches one or more characters. For example, the * wildcard operator matches zero or more characters. You can combine wildcard operators with other characters to create a wildcard pattern.

What is like and Rlike?

LIKE and RLIKE operators are commonly used to filter data based on string patterns. They usually act on a field placed on the left-hand side of the operator, but can also act on a constant (literal) expression. The right-hand side of the operator represents the pattern.

Is Elasticsearch similar to SQL?

It is a component that allows SQL-like queries to be executed in real-time against Elasticsearch. You can think of Elasticsearch SQL as a translator, one that understands both SQL and Elasticsearch and makes it easy to read and process data in real-time, at scale by leveraging Elasticsearch capabilities.


2 Answers

I would highly suggest updating your ElasticSearch version if possible, there have been significant changes since 0.9.x.

This question is not quite specific enough, as there are many ways ElasticSearch can fulfill this functionality, and they differ slightly on your overall goal. If you are looking to replicate that SQL query exactly then in this case use the wildcard query or prefix query.

Using a wildcard query:

Note: Be careful with wildcard searches, they are slow. Avoid using wildcards at the beginning of your strings.

GET /my_index/table_name/_search {     "query": {         "wildcard": {             "field_name": "a*"         }     } } 

Or Prefix query

GET /my_index/table_name/_search {     "query": {         "prefix": {             "field_name": "a"         }     } } 

Or partial matching:

Note: Do NOT blindly use partial matching, while there are corner cases for it's use, correct use of analyzers is almost always better.

Also this exact query will be equivalent to LIKE '%a%', which again, could be better setup with correct use of mapping and a normal query search!

GET /my_index/table_name/_search {     "query": {         "match_phrase": {             "field_name": "a"         }     } } 

If you are reading this wondering about querying ES similarly for search-as-you-type I would suggest reading up on edge-ngrams, which relate to proper use of mapping depending on what you are attempting to do =)

like image 159
Daniel Hoffmann-Mitscherling Avatar answered Oct 19 '22 06:10

Daniel Hoffmann-Mitscherling


GET /indexName/table_name/_search {     "query": {         "match_phrase": {             "field_name": "your partial text"         }     } } 

You can use "type" : "phrase_prefix" to prefix or post fix you search Java code for the same:

AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();  andFilterBuilder.add(FilterBuilders.queryFilter(QueryBuilders.matchPhraseQuery("field_name",           "your partial text"))); 

Gave 'and filter' example so that you can append extra filters if you want to. Check this for more detail:

https://www.elastic.co/guide/en/elasticsearch/guide/current/slop.html

like image 22
Pranav Mishra Avatar answered Oct 19 '22 08:10

Pranav Mishra