# Searching for docs that contain a subset of terms

**URL:** <https://discuss.elastic.co/t/searching-for-docs-that-contain-a-subset-of-terms/120687>\
**Category:** Elasticsearch\
**Created:** [February 20, 2018, 4:56pm UTC](https://discuss.elastic.co/t/searching-for-docs-that-contain-a-subset-of-terms/120687 "2018-02-20T16:56:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Bourne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_bourne/32/53053_2.png) [@Kevin\_Bourne](https://discuss.elastic.co/u/Kevin_Bourne)\
**Post date:** [February 20, 2018, 4:56pm UTC](https://discuss.elastic.co/t/searching-for-docs-that-contain-a-subset-of-terms/120687/1 "2018-02-20T16:56:53Z")

</div>

I've seen this question several times with no adequate answer. What I want to do is to find all documents that contain a subset of values I provide. Example using these docs:

```
{ "_id":1, "values": ["A"] }
{ "_id":2, "values": ["A", "B"] }
{ "_id":3, "values": ["A", "C"] }

```

I want to provide ["A", "D"] and return only the doc with id 1.

I was able to do this in 2.x by making the field a child of the parent document, then a two step search where I first retrieve all the docs that have at least one value not in the provided list (using a "must not"), then second performing a search with any other criteria AND where the docs do not match the ids found in the first list.

I am now moving to ES 5.6.x and wanted to see if there is an easier way of doing this, particularly getting away from having to use a parent-child relationship or the "newer" join relationship.

It looks like ES 6.1 added the "terms set" query but I don't think that is what I want, unless someone can correct me here.

So is there a way to do this and if so do I need any special mapping (such as nested)? Note that I could potentially have many values in both sets so I need a generic solution, not one where there is only 4 values like my example above.

---

<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:** [March 20, 2018, 5:06pm UTC](https://discuss.elastic.co/t/searching-for-docs-that-contain-a-subset-of-terms/120687/2 "2018-03-20T17:06:02Z")

</div>

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