exists-query.asciidoc 2.5 KB

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