How to add LIKE query in Elasticsearch?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Now in this blog i will show you how to add LIKE query in Elasticsearch. If we want to use LIKE query as work on sql, i mean that If we are using a SQL or Mysql ‘Like’ query to get data it will be return data even if its only partially matched. When we are search just ‘t’ then it will return the all records match with ‘t’. So here we can add LIKE statement by using ‘query_string’.

Now we can easily implement LIKE statement by using ‘query_string’, in following example so we can see how to implement:

$search = 't';
$sql = json_decode(' {
            "type": "main",
            "query_cache": true,
            "body": {
                "size": 500,
                "query": {
                    "query_string": {
                        "default_field": "name",
                        "query": "' . $search . ' OR *' . $search . '*",
                        "use_dis_max": true,
                        "fuzzy_max_expansions": 50,
                        "fuzziness": 1
                    }
                }
            }
        }
        ');

Read Also : How to get element with CSS class name and id using jquery ?

Thanks for read this, I hope it will you. You can also follow us on Facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →