2015-05-05 14:27:52 +08:00
|
|
|
[[query-dsl-exists-query]]
|
2019-07-18 22:18:11 +08:00
|
|
|
=== Exists query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Exists</titleabbrev>
|
|
|
|
++++
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2019-06-12 16:26:26 +08:00
|
|
|
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 `null` or `[]`
|
|
|
|
* The field has `"index" : false` set in the mapping
|
|
|
|
* The length of the field value exceeded an `ignore_above` setting in the mapping
|
|
|
|
* The field value was malformed and `ignore_malformed` was defined in the mapping
|
2019-05-07 21:22:59 +08:00
|
|
|
|
|
|
|
[[exists-query-ex-request]]
|
|
|
|
==== Example request
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2019-09-09 22:45:37 +08:00
|
|
|
[source,console]
|
2019-05-07 21:22:59 +08:00
|
|
|
----
|
2016-05-24 17:58:43 +08:00
|
|
|
GET /_search
|
2013-08-29 07:24:34 +08:00
|
|
|
{
|
2020-07-22 00:24:26 +08:00
|
|
|
"query": {
|
|
|
|
"exists": {
|
|
|
|
"field": "user"
|
2017-03-31 10:01:07 +08:00
|
|
|
}
|
2020-07-22 00:24:26 +08:00
|
|
|
}
|
2017-03-31 10:01:07 +08:00
|
|
|
}
|
2019-05-07 21:22:59 +08:00
|
|
|
----
|
2014-11-08 23:57:41 +08:00
|
|
|
|
2019-05-07 21:22:59 +08:00
|
|
|
[[exists-query-top-level-params]]
|
|
|
|
==== Top-level parameters for `exists`
|
|
|
|
`field`::
|
2019-08-01 02:18:22 +08:00
|
|
|
(Required, string) Name of the field you wish to search.
|
2019-05-07 21:22:59 +08:00
|
|
|
+
|
2019-12-23 23:35:14 +08:00
|
|
|
While a field is deemed non-existent if the JSON value is `null` or `[]`, these
|
2019-08-01 02:18:22 +08:00
|
|
|
values will indicate the field does exist:
|
2019-05-07 21:22:59 +08:00
|
|
|
+
|
|
|
|
* Empty strings, such as `""` or `"-"`
|
|
|
|
* Arrays containing `null` and another value, such as `[null, "foo"]`
|
|
|
|
* A custom <<null-value, `null-value`>>, defined in field mapping
|
|
|
|
|
|
|
|
[[exists-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[find-docs-null-values]]
|
2019-06-12 16:26:26 +08:00
|
|
|
===== Find documents missing indexed values
|
|
|
|
To find documents that are missing an indexed value for a field,
|
2019-05-07 21:22:59 +08:00
|
|
|
use the `must_not` <<query-dsl-bool-query, boolean query>> with the `exists`
|
|
|
|
query.
|
|
|
|
|
2019-06-12 16:26:26 +08:00
|
|
|
The following search returns documents that are missing an indexed value for
|
2020-08-04 00:49:56 +08:00
|
|
|
the `user.id` field.
|
2014-11-08 23:57:41 +08:00
|
|
|
|
2019-09-09 22:45:37 +08:00
|
|
|
[source,console]
|
2019-05-07 21:22:59 +08:00
|
|
|
----
|
2016-05-24 17:58:43 +08:00
|
|
|
GET /_search
|
|
|
|
{
|
2020-07-22 00:24:26 +08:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must_not": {
|
|
|
|
"exists": {
|
2020-08-04 00:49:56 +08:00
|
|
|
"field": "user.id"
|
2015-12-10 19:55:05 +08:00
|
|
|
}
|
2020-07-22 00:24:26 +08:00
|
|
|
}
|
2015-12-10 19:55:05 +08:00
|
|
|
}
|
2020-07-22 00:24:26 +08:00
|
|
|
}
|
2015-12-10 19:55:05 +08:00
|
|
|
}
|
2019-05-07 21:22:59 +08:00
|
|
|
----
|