WARNING: Version 6.2 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Exists Query
editExists Query
editReturns documents that have at least one non-null
value in the original field:
GET /_search { "query": { "exists" : { "field" : "user" } } }
For instance, these documents would all match the above query:
An empty string is a non- |
|
Even though the |
|
At least one non- |
These documents would not match the above query:
This field has no values. |
|
At least one non- |
|
The |
null_value
mapping
editIf the field mapping includes the null_value
setting
then explicit null
values are replaced with the specified null_value
. For
instance, if the user
field were mapped as follows:
PUT /example { "mappings": { "doc": { "properties": { "user": { "type": "keyword", "null_value": "_null_" } } } } }
then explicit null
values would be indexed as the string _null_
, and the
following docs would match the exists
filter:
{ "user": null } { "user": [null] }
However, these docs—without explicit null
values—would still have
no values in the user
field and thus would not match the exists
filter:
{ "user": [] } { "foo": "bar" }
missing
query
editThere isn’t a missing
query. Instead use the exists
query inside a
must_not
clause as follows:
GET /_search { "query": { "bool": { "must_not": { "exists": { "field": "user" } } } } }
This query returns documents that have no value in the user field.