# How to match documents that don't have a specific nested object

**URL:** <https://discuss.elastic.co/t/how-to-match-documents-that-dont-have-a-specific-nested-object/249514>\
**Category:** Elasticsearch\
**Created:** [September 22, 2020, 12:10pm UTC](https://discuss.elastic.co/t/how-to-match-documents-that-dont-have-a-specific-nested-object/249514 "2020-09-22T12:10:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomashiron](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@thomashiron](https://discuss.elastic.co/u/thomashiron)\
**Post date:** [September 22, 2020, 12:10pm UTC](https://discuss.elastic.co/t/how-to-match-documents-that-dont-have-a-specific-nested-object/249514/1 "2020-09-22T12:10:04Z")

</div>

Hi!

I'd like to find documents with nested objects that don't match a specific query.  
For example, I want to match documents that don't have an Alice user.

If I follow [the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html), and execute the query below, I still have the document because John Smith doesn't contain Alice:

```auto
GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must_not": [
            { "match": { "user.first": "Alice" }}
          ]
        }
      }
    }
  }
}

```

Result (that should be empty):

```auto
"hits" : {
  "total" : {
    "value" : 1,
    "relation" : "eq"
  },
  "max_score" : 0.0,
  "hits" : [
    {
      "_index" : "my-index-000001",
      "_type" : "_doc",
      "_id" : "1",
      "_score" : 0.0,
      "_source" : {
        "group" : "fans",
        "user" : [
          {
            "first" : "John",
            "last" : "Smith"
          },
          {
            "first" : "Alice",
            "last" : "White"
          }
        ]
      }
    }
  ]
}

```

How can I manage that?

Thanks by advance!

---

<div class="post-metadata">

**Author:** ![thomashiron](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@thomashiron](https://discuss.elastic.co/u/thomashiron)\
**Post date:** [September 22, 2020, 1:34pm UTC](https://discuss.elastic.co/t/how-to-match-documents-that-dont-have-a-specific-nested-object/249514/2 "2020-09-22T13:34:20Z")

</div>

I solved it with this query:

```auto
GET my-index-000001/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "user",
            "query": {
              "match": {
                "user.first": "Alice"
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<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 20, 2020, 1:34pm UTC](https://discuss.elastic.co/t/how-to-match-documents-that-dont-have-a-specific-nested-object/249514/3 "2020-10-20T13:34:32Z")

</div>

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