# Array search

**URL:** <https://discuss.elastic.co/t/array-search/205682>\
**Category:** Elasticsearch\
**Created:** [October 29, 2019, 1:42pm UTC](https://discuss.elastic.co/t/array-search/205682 "2019-10-29T13:42:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![privatechen](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@privatechen](https://discuss.elastic.co/u/privatechen)\
**Post date:** [October 29, 2019, 1:42pm UTC](https://discuss.elastic.co/t/array-search/205682/1 "2019-10-29T13:42:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 1, 2019, 10:46am UTC](https://discuss.elastic.co/t/array-search/205682/2 "2019-11-01T10:46:08Z")

</div>

Take a look at the [terms set query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html) - 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html#terms-set-query-script).

For example, given this index and these documents:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 29, 2019, 10:46am UTC](https://discuss.elastic.co/t/array-search/205682/3 "2019-11-29T10:46:10Z")

</div>

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