# Not exists query

**URL:** <https://discuss.elastic.co/t/not-exists-query/284052>\
**Category:** Elasticsearch\
**Created:** [September 13, 2021, 10:32am UTC](https://discuss.elastic.co/t/not-exists-query/284052 "2021-09-13T10:32:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilgrosso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ilgrosso/32/94485_2.png) [@ilgrosso](https://discuss.elastic.co/u/ilgrosso)\
**Post date:** [September 13, 2021, 10:32am UTC](https://discuss.elastic.co/t/not-exists-query/284052/1 "2021-09-13T10:32:19Z")

</div>

Negating the exists query [as suggested by docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#find-docs-null-values) does not seem to work as I would intend it, at least with Elasticsearch 7.14.

When I send GET to `/master_user/_search` with payload

```auto
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "the_field"
        }
      }
    }
  }
}

```

all hits are returned, including the ones where `the_field` has an actual value.

If instead I am sending GET to `/master_user/_search?q=NOT _exists_:the_field` with no payload, the expected hits are returned.

My issue in particular is that I am generating the JSON payload reported above via Java client as

```auto
QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("the_field"))

```

and have no clue about how to generate the working call.

Any hint?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 14, 2021, 8:04am UTC](https://discuss.elastic.co/t/not-exists-query/284052/2 "2021-09-14T08:04:07Z")

</div>

Can you share a full example. I just tried this under 7.14 and it worked for me

```auto
DELETE test

PUT test/_doc/1
{
  "key" : "value"
}

PUT test/_doc/2?refresh
{
  "key2" : "value"
}

GET test/_search 
{
  "query": {
    "bool": {
      "must_not": [
        { "exists": { "field": "key"}}
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ilgrosso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ilgrosso/32/94485_2.png) [@ilgrosso](https://discuss.elastic.co/u/ilgrosso)\
**Post date:** [September 14, 2021, 9:27am UTC](https://discuss.elastic.co/t/not-exists-query/284052/3 "2021-09-14T09:27:42Z")

</div>

Thank you for your answer.

I did verify on other environments and got similar results as you did: maybe our instance (Elastic Cloud) needs to be re-indexed?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 14, 2021, 9:40am UTC](https://discuss.elastic.co/t/not-exists-query/284052/4 "2021-09-14T09:40:50Z")

</div>

We can debug further using the explain API. See [Explain API | Elasticsearch Guide [7.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-explain.html)

---

<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 12, 2021, 9:41am UTC](https://discuss.elastic.co/t/not-exists-query/284052/5 "2021-10-12T09:41:15Z")

</div>

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