# How to get only documents that exact match with array of values?

**URL:** <https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354>\
**Category:** Elasticsearch\
**Created:** [May 25, 2018, 6:51pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354 "2018-05-25T18:51:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 25, 2018, 6:51pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/1 "2018-05-25T18:51:49Z")

</div>

How to get only documents that exact match with array of values?. for ex:  
I have a document,

doc 1:  
{  
"names":["a","b","c"]  
}

doc 2:  
{  
"names":["a","c"]  
}

search query:  
{  
"names":["a","c"]  
}  
from this query i need only doc2(exactly match) should be returned as a result but I am getting both docs as result. I tried 'terms', must & must\_not but no luck. Can anyone guide me how to achieve this?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [May 25, 2018, 8:55pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/2 "2018-05-25T20:55:14Z")

</div>

Index number of elements and use [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html)

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 26, 2018, 4:53am UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/3 "2018-05-26T04:53:48Z")

</div>

Mikhail, Thanks for your reply. This is was i tried..  
PUT lp\_data\_index  
{  
"settings": { },  
"mappings": {  
"composite": {  
"properties": {  
"required\_matches": {  
"type": "long"  
},  
"data": {  
"type": "keyword"  
}  
}  
}  
}  
}

PUT lp\_data\_index/composite/1  
{  
"data": [  
"cat",  
"tiger"  
],  
"required\_matches": 2  
}

PUT lp\_data\_index/composite/2  
{  
"data": [  
"cat"  
],  
"required\_matches": 1  
}

So, if search for

GET lp\_data\_index/composite/\_search  
{  
"query": {  
"terms\_set": {  
"data" : {  
"terms" : ["cat"],  
"minimum\_should\_match\_field": "required\_matches"  
}  
}  
}  
}

I'm getting 1 response, this is perfectly fine  
"hits": {  
"total": 1,  
"max\_score": 0.2876821,  
"hits": [  
{  
"\_index": "lp\_data\_index",  
"\_type": "composite",  
"\_id": "2",  
"\_score": 0.2876821,  
"\_source": {  
"data": [  
"cat"  
],  
"required\_matches": 1  
}  
}  
]  
}

But when search for  
GET lp\_data\_index/composite/\_search  
{  
"query": {  
"terms\_set": {  
"data" : {  
"terms" : ["cat","tiger"],  
"minimum\_should\_match\_field": "required\_matches"  
}  
}  
}  
}

Im getting both docs, but im expecting only one doc should be in result  
"hits": {  
"total": 2,  
"max\_score": 0.5753642,  
"hits": [  
{  
"\_index": "lp\_data\_index",  
"\_type": "composite",  
"\_id": "1",  
"\_score": 0.5753642,  
"\_source": {  
"data": [  
"cat",  
"tiger"  
],  
"required\_matches": 2  
}  
},  
{  
"\_index": "lp\_data\_index",  
"\_type": "composite",  
"\_id": "2",  
"\_score": 0.2876821,  
"\_source": {  
"data": [  
"cat"  
],  
"required\_matches": 1  
}  
}  
]  
}

So in second case also how to get exactly matching doc?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [May 26, 2018, 10:59am UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/4 "2018-05-26T10:59:38Z")

</div>

What does \_explain respond?

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 26, 2018, 12:16pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/5 "2018-05-26T12:16:04Z")

</div>

Sorry couldn't get you,  
for this search,

{  
"query": {  
"terms\_set": {  
"data": {  
"minimum\_should\_match\_field": "required\_matches",  
"terms": [  
"cat",  
"tiger"  
]  
}  
}  
}  
}

for this search I expected only one doc(id:1) in the result, but I'm getting both docs(id:1 & id:2).

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [May 26, 2018, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/6 "2018-05-26T12:30:23Z")

</div>

Ok. This mm\_field works a little contraintuitive. Here we go  
$ curl -H 'Content-Type: application/json' [http://localhost:9200/lp\_data\_index/composite/\_search?pretty](http://localhost:9200/lp_data_index/composite/_search?pretty) -d '

{

"query": {

"bool":{

```
 "must": [{

```

"terms\_set": {

"data" : {

"terms" : ["cat","tiger"],

"minimum\_should\_match\_field": "required\_matches"

}

}

}], "filter":[{

```
 "range": { "required_matches": { "gte": "2" }}

}]

```

}

}

}'

{

"took" : 15,

"timed\_out" : false,

"\_shards" : {

```
"total" : 5,

"successful" : 5,

"skipped" : 0,

"failed" : 0

```

},

"hits" : {

```
"total" : 1,

"max_score" : 0.5753642,

"hits" : [

  {

    "_index" : "lp_data_index",

    "_type" : "composite",

    "_id" : "1",

    "_score" : 0.5753642,

    "_source" : {

      "data" : [

        "cat",

        "tiger"

      ],

      "required_matches" : 2

    }

  }

]

```

}

}

Depending on the problem you might replace \>= range to exact equality.

Note: in the previous question I mean the way to debug such issues [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html).

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 26, 2018, 12:32pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/7 "2018-05-26T12:32:02Z")

</div>

Thanks Mikhail.

---

<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:** [June 23, 2018, 12:32pm UTC](https://discuss.elastic.co/t/how-to-get-only-documents-that-exact-match-with-array-of-values/133354/8 "2018-06-23T12:32:10Z")

</div>

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