简体   繁体   中英

How do I match multiple values for array data types in Elasticsearch?

Considering the following data set, how would I match on multiple values of the same field?

{
  "description": "My first description.",
  "tags": ["A", "B", "C"]
},
{
  "description": "My second description.",
  "tags": ["A", "D", "E"]
}

In the above, I would like to create a query that matches on records with "tags" of "A" and "B". I'm currently using something similar to the following:

query: {
  terms: {
    tags: ["A", "B"]
  }
}

However, this will return both records, as it's matching on any of the values. Is there any ability to keep this dynamic (pass in array of values) and return results that include all of the "tags"?

Since version 6.1, you can use the terms_set query (see documentation ), which allows you to write your query as follows:

{
  "query": {
    "terms_set": {
      "score": {
        "tags": ["A","B"],
        "minimum_should_match_script": {
          "source": "2"
        }
      }
    }
  }
}

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