Array search

for example, I got three items like:
{"arr":["a", "b", "c"]},
{"arr":["a", "b"]},
{"arr":["a", "c"]}

my search like this:

"query":{
"bool":{
"must":[
{
"term":{
"arr":{
"value":"a"
}
}
},
{
"term":{
"arr":{
"value":"b"
}
}
}
],
"minimum_should_match": 2
}
},

I just want to get ['a', 'b'] but also get ['a', 'b', 'c'].
I google a lot , but cannot find a way to solve this.
Is elasticsearch has this ability to do this?

Take a look at the terms set query - it does exactly what you're looking for.

It allows you to define - per document - how many terms need to match. You can do that dynamically, per document, using a script.

For example, given this index and these documents:

PUT /my_index
{
  "mappings": {
    "properties": {
      "arr": {
        "type": "keyword"
      }
    }
  }
}

PUT my_index/_doc/1
{
  "arr": [
    "a",
    "b",
    "c"
  ]
}

PUT my_index/_doc/2
{
  "arr": [
    "a",
    "b"
  ]
}

PUT my_index/_doc/3
{
  "arr": [
    "a",
    "c"
  ]
}

You can use the following terms set query to only match document 2:

GET /my_index/_search
{
  "query": {
    "terms_set": {
      "arr": {
        "terms": [
          "a",
          "b"
        ],
        "minimum_should_match_script": {
          "source": "Math.max(params.num_terms, doc['arr'].size())"
        }
      }
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.