Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get elasticsearch to perform similar to SQL 'LIKE'

If using a SQL 'Like' statement to query data it will return data even if its only partially matched. For instance, if I'm searching for food and there's an item in my db called "raisins" when using SQL the query would return "raisins" even if my search only contained "rai". In elasticsearch, the query won't return a record unless the entire name (in this case "raisins") is specified. How can I get elasticsearch to behave similar to the SQL statement. I'm using Rails 3.1.1 and PostgreSQL. Thanks!

like image 971
Steve Avatar asked May 18 '12 06:05

Steve


1 Answers

While creating index of model for elasticsearch use tokenizer on index which will fulfil your requirement. For. e.g.

tokenizer: {
              :name_tokenizer => {type: "edgeNGram", max_gram: 100, min_gram: 3, side: "front"}
           }

this will create tokens of size from 3 to 100 of your fields and as side is given as front it will check from the starting. You can get more details from here http://www.slideshare.net/clintongormley/terms-of-endearment-the-elasticsearch-query-dsl-explained

like image 132
abhas Avatar answered Nov 16 '22 07:11

abhas