# Nested Query Help to find documents where all nested documents match a given criteria

**URL:** <https://discuss.elastic.co/t/nested-query-help-to-find-documents-where-all-nested-documents-match-a-given-criteria/200923>\
**Category:** Elasticsearch\
**Created:** [September 24, 2019, 6:29pm UTC](https://discuss.elastic.co/t/nested-query-help-to-find-documents-where-all-nested-documents-match-a-given-criteria/200923 "2019-09-24T18:29:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)\
**Post date:** [September 24, 2019, 6:29pm UTC](https://discuss.elastic.co/t/nested-query-help-to-find-documents-where-all-nested-documents-match-a-given-criteria/200923/1 "2019-09-24T18:29:59Z")

</div>

Hi,

I was wondering if it is possible to find documents in ElasticSearch using a nested query where all nested documents within a document match a given criteria?

Thanks.

---

<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:** [September 26, 2019, 1:48pm UTC](https://discuss.elastic.co/t/nested-query-help-to-find-documents-where-all-nested-documents-match-a-given-criteria/200923/2 "2019-09-26T13:48:59Z")

</div>

A nested query will return a document if **any** nested objected matches. But you're saying you only want to find documents where **all** nested objects match?

There may be more elegant ways of doing this, but one approach I can think of is by using a nested `must_not` clause inside another `must_not` clause. Or in other words: find documents that do not have a nested object that does not match the query.

For example, given this mapping and these documents:

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "my_nested_field": {
        "type": "nested",
        "properties": {
          "my_field": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "my_nested_field": [
    {
      "my_field": "a"
    },
    {
      "my_field": "a"
    }
  ]
}

PUT my_index/_doc/2
{
  "my_nested_field": [
    {
      "my_field": "a"
    },
    {
      "my_field": "b"
    }
  ]
}

```

This is what that query would look like:

```auto
GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "my_nested_field",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "match": {
                      "my_nested_field.my_field": "a"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<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 24, 2019, 1:49pm UTC](https://discuss.elastic.co/t/nested-query-help-to-find-documents-where-all-nested-documents-match-a-given-criteria/200923/3 "2019-10-24T13:49:03Z")

</div>

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