# Elasticsearch 5.x locate documents without nested field

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-x-locate-documents-without-nested-field/105478>\
**Category:** Elasticsearch\
**Created:** [October 26, 2017, 9:02pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-locate-documents-without-nested-field/105478 "2017-10-26T21:02:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_boston](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@elastic\_boston](https://discuss.elastic.co/u/elastic_boston)\
**Post date:** [October 26, 2017, 9:02pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-locate-documents-without-nested-field/105478/1 "2017-10-26T21:02:36Z")

</div>

0  
down vote  
favorite  
I have an index with a nested object. Mapping is like -

```
{
  "mappings": {
    "my_type": {
      "properties": {
        "group": {"type": "string"},
        "users": {
          "type": "nested", 
           "properties": {
                 "first": {"type": "string"},
                 "last": {"type": "string"}
           }
        }
      }
    }
  }
}

```

I need to find all documents which have no 'users'. I can see how to find only document which have users -

```
GET /my_index/my_type/_search
{
  "query": {
      "nested": {
          "path": "users",
             "query": {
                 "bool": {
                    "must": [
                     {
                      "exists": {
                           "field": "users"
                          }
                      }
                  ]
                 }
          }
       }
    }
 }

```

but I don't know how to do the reverse. Any pointers?

---

<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:** [October 28, 2017, 11:07pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-locate-documents-without-nested-field/105478/2 "2017-10-28T23:07:39Z")

</div>

Wrap your nested query in a `must_not`. The following should work:

```
GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "query": {
            "nested": {
              "path": "users",
              "query": {
                "exists": {
                  "field": "users"
                }
              }
            }
          }
        }
      ]
    }
  }
}
```

---

<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:** [November 25, 2017, 11:07pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-locate-documents-without-nested-field/105478/3 "2017-11-25T23:07:43Z")

</div>

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