简体   繁体   中英

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?

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 =)

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" } }
                    ] 
          } 
    } 
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM