# Find documents that only have terms intersecting a list of terms but no other terms

**URL:** <https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194>\
**Category:** Elasticsearch\
**Created:** [October 28, 2015, 9:48pm UTC](https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194 "2015-10-28T21:48:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamFeuer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adamfeuer/32/5542_2.png) [@AdamFeuer](https://discuss.elastic.co/u/AdamFeuer)\
**Post date:** [October 28, 2015, 9:48pm UTC](https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194/1 "2015-10-28T21:48:01Z")

</div>

I have documents that have a list of labels:

```
{
   "fields": {
      "label": [
           "foo",
           "bar",
           "baz"
      ],
      "name": [
         "Document One"
      ],
      "description" : "A fine first document",
      "id" : 1
   }
},
{
   "fields": {
      "label": [
           "foo",
           "dog"
      ],
      "name": [
         "Document Two"
      ],
      "description" : "A fine second document",
      "id" : 2
   }
}

```

I have a list of terms:

```
["foo", "bar", "qux", "zip", "baz"]

```

I want a query that will return documents that have labels in the list of terms - but no other terms.

So given the list above, the query would return Document One, but not Document Two (because it has the term dog that is not in the list of terms.

I was able to achieve this with a Groovy script ([here's the details in a StackOverflow answer](http://stackoverflow.com/a/31421254/431222)).

However, we're interested in using Amazon's new Elasticsearch service offering. This does not allow dynamic scripting.

Is there another way to accomplish this, without using scripting?

## cheers adam

Adam Feuer  
CookBrite, Inc.  
Seattle, WA, USA

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [October 30, 2015, 10:37am UTC](https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194/2 "2015-10-30T10:37:56Z")

</div>

> [@AdamFeuer](#):
>
> Is there another way to accomplish this, without using scripting?

None that I know of at the top of my head - other than maybe postponing the "filter documents that contain tags not in the list" to the application side. Maybe others can add more here if I'm missing something obvious.

As an aside - not sure if that's an option for you: [Elastic Cloud: Hosted Elasticsearch, Hosted Search | Elastic](https://www.elastic.co/found) provides a hosted ES solution as well and does allow for dynamic scripting.

Isabel

---

<div class="post-metadata">

**Author:** ![andyofb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andyofb/32/5595_2.png) [@andyofb](https://discuss.elastic.co/u/andyofb)\
**Post date:** [October 30, 2015, 3:50pm UTC](https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194/3 "2015-10-30T15:50:01Z")

</div>

I had to do something similar recently and ended up creating parent child indexes and then searching on the children (which would give you only the results you want) and you can query or filter based on the parent document to get the results you are after.

---

<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:** [July 5, 2017, 11:41pm UTC](https://discuss.elastic.co/t/find-documents-that-only-have-terms-intersecting-a-list-of-terms-but-no-other-terms/33194/4 "2017-07-05T23:41:38Z")

</div>


