# Elastic Search query that matches all elements in nested document

**URL:** <https://discuss.elastic.co/t/elastic-search-query-that-matches-all-elements-in-nested-document/307395>\
**Category:** Elasticsearch\
**Created:** [June 16, 2022, 11:26am UTC](https://discuss.elastic.co/t/elastic-search-query-that-matches-all-elements-in-nested-document/307395 "2022-06-16T11:26:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [June 16, 2022, 8:38pm UTC](https://discuss.elastic.co/t/elastic-search-query-that-matches-all-elements-in-nested-document/307395/4 "2022-06-16T20:38:24Z")

</div>

```auto
{
  "query": {
    "bool": {
      "must_not": [ // exclude those parents, which ... 
        {
          "nested": {
            "path": "SecurityCodes",
            "query": {
                "bool": {
                     "must": [ // .. has any child ...
                          "exists": {
                                "field": "SecurityCodes.Name"
                            }
                     ],
                     "must_not": [ // ... but not Restricted
                           { 
                               "match": {
                                     "SecurityCodes.Name": {
                                     "query": "Restricted"
                                  }
                               }
                           }
                     ],
                }
           }
       ]
    }
  }
}

```

Assuming that ES handles top pure negation query, it might do the thing.

---

_[View the full topic](https://discuss.elastic.co/t/elastic-search-query-that-matches-all-elements-in-nested-document/307395)._
