Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match multiple words via terms in elasticsearch

My query for matching multiple words is as following,

{"query":
{"bool":{"must":[{"terms":{"my_field":"word1 word2"}}]}

upon execution, the result set is empty though data exists for the following query. Instead of above query, if I use

{"bool":{"must":[{"terms":{"my_field":"word1"}}]}

then elastic-search is returning data. How to match the complete sentence?

like image 507
Sowmya Avatar asked Jul 26 '18 10:07

Sowmya


People also ask

What is multi match query in Elasticsearch?

The multi_match query provides a convenient shorthand way of running the same query against multiple fields.

How do you use terms in Elasticsearch?

By default, Elasticsearch limits the terms query to a maximum of 65,536 terms. You can change this limit using the index. max_terms_count setting. To use the field values of an existing document as search terms, use the terms lookup parameters.

What is the difference between term and match in Elasticsearch?

To better search text fields, the match query also analyzes your provided search term before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. The term query does not analyze the search term. The term query only searches for the exact term you provide.


2 Answers

Based on your comment on the above answer, I believe you should simply use two term queries inside your must query array.

{
  "query": 
    { "bool" :
          {
             "must":[
                        {"term":{"my_field": "word1" } },
                        {"term":{"my_field": "word2" } }
                    ] 
          } 
    } 
 } 
like image 176
Tushar Shahi Avatar answered Sep 27 '22 22:09

Tushar Shahi


you can try to put the words in an array and see if it works. Like this: {"query": {"bool":{"must":[{"terms":{"my_field":["word1", "word2"]}}]}

here is the documentation: https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html

Hope it works =)

like image 30
David Vela Avatar answered Sep 27 '22 20:09

David Vela