# Search for documents containing an item not present in a list

**URL:** <https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958>\
**Category:** Elasticsearch\
**Created:** [September 22, 2022, 2:38pm UTC](https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958 "2022-09-22T14:38:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_Fresco](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Adrian\_Fresco](https://discuss.elastic.co/u/Adrian_Fresco)\
**Post date:** [September 22, 2022, 2:38pm UTC](https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958/1 "2022-09-22T14:38:09Z")

</div>

I have indexed documents that contain a field `name` with a string value:

```auto
[
    {
        "fruits": [
            {
                "name": "apple",
                "color": "red"
            },
            {
                "name": "banana",
                "color": "yellow"
            }
        ]
    },
    {
        "fruits": [
            {
                "name": "grape",
                "color": "purple"
            },
            {
                "name": "orange",
                "color": "orange"
            },
            {
                "name": "pear",
                "color": "green"
            }
        ]
    }
]

```

I have a list of strings in my code:

```auto
["apple", "pineapple", "grape", "orange", "pear"]

```

I want to search for documents that contain a value in the field `name` that is not present in that list. In this case only the first document would be the result as "banana" is missing.

How can I do that?

Thanks

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 22, 2022, 6:07pm UTC](https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958/2 "2022-09-22T18:07:22Z")

</div>

Hi @Adrian_Fresco

I adapted the query by this [post](https://discuss.elastic.co/t/elastic-search-query-that-matches-all-elements-in-nested-document/307395/4). Try please.

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "fruits",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "terms": {
                      "fruits.name": [
                        "apple",
                        "pineapple",
                        "grape",
                        "orange",
                        "pear"
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Adrian\_Fresco](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Adrian\_Fresco](https://discuss.elastic.co/u/Adrian_Fresco)\
**Post date:** [September 27, 2022, 3:48pm UTC](https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958/3 "2022-09-27T15:48:40Z")

</div>

Thank you for your answer @RabBit_BR  
Is there a way of achieving the result without using nested field mappings?

---

<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:** [October 25, 2022, 3:48pm UTC](https://discuss.elastic.co/t/search-for-documents-containing-an-item-not-present-in-a-list/314958/4 "2022-10-25T15:48:59Z")

</div>

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