<commit_before>[[query-dsl-exists-query]]
=== Exists Query

Returns documents that have at least one non-`null` value in the original field:

[source,js]
--------------------------------------------------
{
    "exists" : { "field" : "user" }
}
--------------------------------------------------

For instance, these documents would all match the above query:

[source,js]
--------------------------------------------------
{ "user": "jane" }
{ "user": "" } <1>
{ "user": "-" } <2>
{ "user": ["jane"] }
{ "user": ["jane", null ] } <3>
--------------------------------------------------
<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.

These documents would *not* match the above query:

[source,js]
--------------------------------------------------
{ "user": null }
{ "user": [] } <1>
{ "user": [null] } <2>
{ "foo":  "bar" } <3>
--------------------------------------------------
<1> This field has no values.
<2> At least one non-`null` value is required.
<3> The `user` field is missing completely.

[float]
===== `null_value` mapping

If the field mapping includes the <<null-value,`null_value`>> setting
then explicit `null` values are replaced with the specified `null_value`.  For
instance, if the `user` field were mapped as follows:

[source,js]
--------------------------------------------------
  "user": {
    "type": "string",
    "null_value": "_null_"
  }
--------------------------------------------------

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] }
--------------------------------------------------

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" }
--------------------------------------------------

===== `missing` query

The exists query can advantageously replace the `missing` query when used inside a must_not clause as follows:

[source,js]
--------------------------------------------------
"bool": {
    "must_not": {
        "exists": {
            "field": "user"
        }
    }
}
--------------------------------------------------

This query returns documents that have no value in the user field.<commit_msg>Fixed asciidoc warning in exists-query.asciidoc<commit_after>[[query-dsl-exists-query]]
=== Exists Query

Returns documents that have at least one non-`null` value in the original field:

[source,js]
--------------------------------------------------
{
    "exists" : { "field" : "user" }
}
--------------------------------------------------

For instance, these documents would all match the above query:

[source,js]
--------------------------------------------------
{ "user": "jane" }
{ "user": "" } <1>
{ "user": "-" } <2>
{ "user": ["jane"] }
{ "user": ["jane", null ] } <3>
--------------------------------------------------
<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.

These documents would *not* match the above query:

[source,js]
--------------------------------------------------
{ "user": null }
{ "user": [] } <1>
{ "user": [null] } <2>
{ "foo":  "bar" } <3>
--------------------------------------------------
<1> This field has no values.
<2> At least one non-`null` value is required.
<3> The `user` field is missing completely.

[float]
==== `null_value` mapping

If the field mapping includes the <<null-value,`null_value`>> setting
then explicit `null` values are replaced with the specified `null_value`.  For
instance, if the `user` field were mapped as follows:

[source,js]
--------------------------------------------------
  "user": {
    "type": "string",
    "null_value": "_null_"
  }
--------------------------------------------------

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] }
--------------------------------------------------

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" }
--------------------------------------------------

==== `missing` query

The exists query can advantageously replace the `missing` query when used inside a must_not clause as follows:

[source,js]
--------------------------------------------------
"bool": {
    "must_not": {
        "exists": {
            "field": "user"
        }
    }
}
--------------------------------------------------

This query returns documents that have no value in the user field.
{'fraction_non_alphanumeric': 0.3839875631558492, 'fraction_numerical': 0.004663816556548776, 'mean_word_length': 1.3470132238942087, 'pattern_counts': {'":': 40, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7478665', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1285, 'n_words': 481}