Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching field with multiple values

I want to get all documents that match the words: Apples, Oranges, and Pineapples in the field Fruit. So basically I want something like this:

{
  "query":{
    "bool":{
      "must": [
        {
          "match": {
            "fruits": ["Apples","Oranges","Pineapples"]
          }
        }
      ]
    }
  }
}

How do I accomplish this with a simple trick like that?

like image 484
user2896120 Avatar asked Oct 24 '25 05:10

user2896120


1 Answers

You can use "terms" in place of "match" to get the documents

{
   "query": {
      "terms": {
         "fruits": [
            "apples",
            "oranges",
            "pineapples"
         ]
      }
   }
}

Note: You should lowercase the values because "terms" query find exact matching and if no analyser is used default analyser (Standard Analyzer) is used by elasticsearch while indexing text values which lowercased the values before indexing it

like image 174
bornTalented Avatar answered Oct 26 '25 21:10

bornTalented