Not exists query

Negating the exists query as suggested by docs 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

{
  "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

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

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

Any hint?

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

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"}}
      ]
    }
  }
}

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?

We can debug further using the explain API. See Explain API | Elasticsearch Guide [7.14] | Elastic

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