Exists query
Returns documents that contain an indexed value for a field.
An indexed value may not exist for a document’s field due to a variety of reasons:
- The field in the source JSON is
nullor[] - The field has
"index" : falseand"doc_values" : falseset in the mapping - The length of the field value exceeded an
ignore_abovesetting in the mapping - The field value was malformed and
ignore_malformedwas defined in the mapping
Example request
resp = client.search(
query={
"exists": {
"field": "user"
}
},
)
print(resp) response = client.search(
body: {
query: {
exists: {
field: 'user'
}
}
}
)
puts response res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"exists": {
"field": "user"
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err) const response = await client.search({
query: {
exists: {
field: "user",
},
},
});
console.log(response); GET /_search
{
"query": {
"exists": {
"field": "user"
}
}
} Top-level parameters for exists
-
field -
(Required, string) Name of the field you wish to search.
While a field is deemed non-existent if the JSON value is
nullor[], these values will indicate the field does exist:- Empty strings, such as
""or"-" - Arrays containing
nulland another value, such as[null, "foo"] - A custom
null-value, defined in field mapping
- Empty strings, such as
Notes
Find documents missing indexed values
To find documents that are missing an indexed value for a field, use the must_not boolean query with the exists query.
The following search returns documents that are missing an indexed value for the user.id field.
resp = client.search(
query={
"bool": {
"must_not": {
"exists": {
"field": "user.id"
}
}
}
},
)
print(resp) response = client.search(
body: {
query: {
bool: {
must_not: {
exists: {
field: 'user.id'
}
}
}
}
}
)
puts response const response = await client.search({
query: {
bool: {
must_not: {
exists: {
field: "user.id",
},
},
},
},
});
console.log(response); GET /_search
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "user.id"
}
}
}
}
}
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/query-dsl-exists-query.html