2015-05-05 02:27:52 -04:00
|
|
|
[[query-dsl-exists-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Exists Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-11-08 10:57:41 -05:00
|
|
|
Returns documents that have at least one non-`null` value in the original field:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"exists" : { "field" : "user" }
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-05-05 02:27:52 -04:00
|
|
|
For instance, these documents would all match the above query:
|
2014-11-08 10:57:41 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{ "user": "jane" }
|
|
|
|
{ "user": "" } <1>
|
|
|
|
{ "user": "-" } <2>
|
|
|
|
{ "user": ["jane"] }
|
|
|
|
{ "user": ["jane", null ] } <3>
|
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
// NOTCONSOLE
|
2014-11-08 10:57:41 -05:00
|
|
|
<1> An empty string is a non-`null` value.
|
|
|
|
<2> Even though the `standard` analyzer would emit zero tokens, the original field is non-`null`.
|
|
|
|
<3> At least one non-`null` value is required.
|
|
|
|
|
2015-05-05 02:27:52 -04:00
|
|
|
These documents would *not* match the above query:
|
2014-11-08 10:57:41 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{ "user": null }
|
|
|
|
{ "user": [] } <1>
|
|
|
|
{ "user": [null] } <2>
|
|
|
|
{ "foo": "bar" } <3>
|
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
// NOTCONSOLE
|
2014-11-08 10:57:41 -05:00
|
|
|
<1> This field has no values.
|
|
|
|
<2> At least one non-`null` value is required.
|
|
|
|
<3> The `user` field is missing completely.
|
|
|
|
|
|
|
|
[float]
|
2015-12-10 06:55:05 -05:00
|
|
|
==== `null_value` mapping
|
2014-11-08 10:57:41 -05:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
If the field mapping includes the <<null-value,`null_value`>> setting
|
2014-11-08 10:57:41 -05:00
|
|
|
then explicit `null` values are replaced with the specified `null_value`. For
|
|
|
|
instance, if the `user` field were mapped as follows:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
PUT /example
|
|
|
|
{
|
2017-03-30 22:21:02 -04:00
|
|
|
"mappings": {
|
2018-10-22 14:54:04 -04:00
|
|
|
"_doc": {
|
2017-03-30 22:01:07 -04:00
|
|
|
"properties": {
|
|
|
|
"user": {
|
2017-03-30 22:21:02 -04:00
|
|
|
"type": "keyword",
|
2017-03-30 22:01:07 -04:00
|
|
|
"null_value": "_null_"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 10:57:41 -05:00
|
|
|
}
|
2017-03-30 22:01:07 -04:00
|
|
|
}
|
2014-11-08 10:57:41 -05:00
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
// CONSOLE
|
2014-11-08 10:57:41 -05:00
|
|
|
|
|
|
|
then explicit `null` values would be indexed as the string `_null_`, and the
|
|
|
|
following docs would match the `exists` filter:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{ "user": null }
|
|
|
|
{ "user": [null] }
|
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
// NOTCONSOLE
|
2014-11-08 10:57:41 -05:00
|
|
|
|
|
|
|
However, these docs--without explicit `null` values--would still have
|
|
|
|
no values in the `user` field and thus would not match the `exists` filter:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{ "user": [] }
|
|
|
|
{ "foo": "bar" }
|
|
|
|
--------------------------------------------------
|
2017-03-30 22:01:07 -04:00
|
|
|
// NOTCONSOLE
|
2014-11-08 10:57:41 -05:00
|
|
|
|
2015-12-10 06:55:05 -05:00
|
|
|
==== `missing` query
|
|
|
|
|
2017-03-30 22:01:07 -04:00
|
|
|
There isn't a `missing` query. Instead use the `exists` query inside a
|
|
|
|
`must_not` clause as follows:
|
2015-12-10 06:55:05 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"must_not": {
|
|
|
|
"exists": {
|
|
|
|
"field": "user"
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 06:55:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2015-12-10 06:55:05 -05:00
|
|
|
|
|
|
|
This query returns documents that have no value in the user field.
|