exists-query.asciidoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. <1> An empty string is a non-`null` value.
  24. <2> Even though the `standard` analyzer would emit zero tokens, the original field is non-`null`.
  25. <3> At least one non-`null` value is required.
  26. These documents would *not* match the above query:
  27. [source,js]
  28. --------------------------------------------------
  29. { "user": null }
  30. { "user": [] } <1>
  31. { "user": [null] } <2>
  32. { "foo": "bar" } <3>
  33. --------------------------------------------------
  34. <1> This field has no values.
  35. <2> At least one non-`null` value is required.
  36. <3> The `user` field is missing completely.
  37. [float]
  38. ==== `null_value` mapping
  39. If the field mapping includes the <<null-value,`null_value`>> setting
  40. then explicit `null` values are replaced with the specified `null_value`. For
  41. instance, if the `user` field were mapped as follows:
  42. [source,js]
  43. --------------------------------------------------
  44. "user": {
  45. "type": "text",
  46. "null_value": "_null_"
  47. }
  48. --------------------------------------------------
  49. then explicit `null` values would be indexed as the string `_null_`, and the
  50. following docs would match the `exists` filter:
  51. [source,js]
  52. --------------------------------------------------
  53. { "user": null }
  54. { "user": [null] }
  55. --------------------------------------------------
  56. However, these docs--without explicit `null` values--would still have
  57. no values in the `user` field and thus would not match the `exists` filter:
  58. [source,js]
  59. --------------------------------------------------
  60. { "user": [] }
  61. { "foo": "bar" }
  62. --------------------------------------------------
  63. ==== `missing` query
  64. 'missing' query has been removed because it can be advantageously replaced by an `exists` query inside a must_not
  65. clause as follows:
  66. [source,js]
  67. --------------------------------------------------
  68. GET /_search
  69. {
  70. "query": {
  71. "bool": {
  72. "must_not": {
  73. "exists": {
  74. "field": "user"
  75. }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. This query returns documents that have no value in the user field.