2015-05-05 02:27:52 -04:00
|
|
|
[[query-dsl-exists-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Exists query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Exists</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-06-12 04:26:26 -04: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 09:22:59 -04:00
|
|
|
|
|
|
|
[[exists-query-ex-request]]
|
|
|
|
==== Example request
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-05-07 09:22:59 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"exists": {
|
|
|
|
"field": "user"
|
2017-03-30 22:01:07 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2017-03-30 22:01:07 -04:00
|
|
|
}
|
2019-05-07 09:22:59 -04:00
|
|
|
----
|
2014-11-08 10:57:41 -05:00
|
|
|
|
2019-05-07 09:22:59 -04:00
|
|
|
[[exists-query-top-level-params]]
|
|
|
|
==== Top-level parameters for `exists`
|
|
|
|
`field`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, string) Name of the field you wish to search.
|
2019-05-07 09:22:59 -04:00
|
|
|
+
|
2019-12-23 12:38:17 -05:00
|
|
|
While a field is deemed non-existent if the JSON value is `null` or `[]`, these
|
2019-07-31 14:18:22 -04:00
|
|
|
values will indicate the field does exist:
|
2019-05-07 09:22:59 -04: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 04:26:26 -04:00
|
|
|
===== Find documents missing indexed values
|
|
|
|
To find documents that are missing an indexed value for a field,
|
2019-05-07 09:22:59 -04:00
|
|
|
use the `must_not` <<query-dsl-bool-query, boolean query>> with the `exists`
|
|
|
|
query.
|
|
|
|
|
2019-06-12 04:26:26 -04:00
|
|
|
The following search returns documents that are missing an indexed value for
|
2020-08-03 13:31:19 -04:00
|
|
|
the `user.id` field.
|
2014-11-08 10:57:41 -05:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-05-07 09:22:59 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must_not": {
|
|
|
|
"exists": {
|
2020-08-03 13:31:19 -04:00
|
|
|
"field": "user.id"
|
2015-12-10 06:55:05 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2015-12-10 06:55:05 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2015-12-10 06:55:05 -05:00
|
|
|
}
|
2019-05-07 09:22:59 -04:00
|
|
|
----
|