exists-query.asciidoc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "constant_score" : {
  8. "filter" : {
  9. "exists" : { "field" : "user" }
  10. }
  11. }
  12. }
  13. --------------------------------------------------
  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": "string",
  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. --------------------------------------------------