1
0

exists-query.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. [[query-dsl-exists-query]]
  2. === Exists Query
  3. Returns documents that have at least one non-`null` value in the original field:
  4. [source,js]
  5. --------------------------------------------------
  6. GET /_search
  7. {
  8. "query": {
  9. "exists" : { "field" : "user" }
  10. }
  11. }
  12. --------------------------------------------------
  13. // CONSOLE
  14. For instance, these documents would all match the above query:
  15. [source,js]
  16. --------------------------------------------------
  17. { "user": "jane" }
  18. { "user": "" } <1>
  19. { "user": "-" } <2>
  20. { "user": ["jane"] }
  21. { "user": ["jane", null ] } <3>
  22. --------------------------------------------------
  23. // NOTCONSOLE
  24. <1> An empty string is a non-`null` value.
  25. <2> Even though the `standard` analyzer would emit zero tokens, the original field is non-`null`.
  26. <3> At least one non-`null` value is required.
  27. These documents would *not* match the above query:
  28. [source,js]
  29. --------------------------------------------------
  30. { "user": null }
  31. { "user": [] } <1>
  32. { "user": [null] } <2>
  33. { "foo": "bar" } <3>
  34. --------------------------------------------------
  35. // NOTCONSOLE
  36. <1> This field has no values.
  37. <2> At least one non-`null` value is required.
  38. <3> The `user` field is missing completely.
  39. [float]
  40. ==== `null_value` mapping
  41. If the field mapping includes the <<null-value,`null_value`>> setting
  42. then explicit `null` values are replaced with the specified `null_value`. For
  43. instance, if the `user` field were mapped as follows:
  44. [source,js]
  45. --------------------------------------------------
  46. PUT /example
  47. {
  48. "mappings": {
  49. "doc": {
  50. "properties": {
  51. "user": {
  52. "type": "keyword",
  53. "null_value": "_null_"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // CONSOLE
  61. then explicit `null` values would be indexed as the string `_null_`, and the
  62. following docs would match the `exists` filter:
  63. [source,js]
  64. --------------------------------------------------
  65. { "user": null }
  66. { "user": [null] }
  67. --------------------------------------------------
  68. // NOTCONSOLE
  69. However, these docs--without explicit `null` values--would still have
  70. no values in the `user` field and thus would not match the `exists` filter:
  71. [source,js]
  72. --------------------------------------------------
  73. { "user": [] }
  74. { "foo": "bar" }
  75. --------------------------------------------------
  76. // NOTCONSOLE
  77. ==== `missing` query
  78. There isn't a `missing` query. Instead use the `exists` query inside a
  79. `must_not` clause as follows:
  80. [source,js]
  81. --------------------------------------------------
  82. GET /_search
  83. {
  84. "query": {
  85. "bool": {
  86. "must_not": {
  87. "exists": {
  88. "field": "user"
  89. }
  90. }
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. // CONSOLE
  96. This query returns documents that have no value in the user field.