missing-query.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. [[query-dsl-missing-query]]
  2. == Missing Query
  3. Returns documents that have only `null` values or no value in the original field:
  4. [source,js]
  5. --------------------------------------------------
  6. {
  7. "constant_score" : {
  8. "filter" : {
  9. "missing" : { "field" : "user" }
  10. }
  11. }
  12. }
  13. --------------------------------------------------
  14. For instance, the following docs would match the above filter:
  15. [source,js]
  16. --------------------------------------------------
  17. { "user": null }
  18. { "user": [] } <1>
  19. { "user": [null] } <2>
  20. { "foo": "bar" } <3>
  21. --------------------------------------------------
  22. <1> This field has no values.
  23. <2> This field has no non-`null` values.
  24. <3> The `user` field is missing completely.
  25. These documents would *not* match the above filter:
  26. [source,js]
  27. --------------------------------------------------
  28. { "user": "jane" }
  29. { "user": "" } <1>
  30. { "user": "-" } <2>
  31. { "user": ["jane"] }
  32. { "user": ["jane", null ] } <3>
  33. --------------------------------------------------
  34. <1> An empty string is a non-`null` value.
  35. <2> Even though the `standard` analyzer would emit zero tokens, the original field is non-`null`.
  36. <3> This field has one non-`null` value.
  37. [float]
  38. === `null_value` mapping
  39. If the field mapping includes a `null_value` (see <<mapping-core-types>>) then explicit `null` values
  40. are replaced with the specified `null_value`. For instance, if the `user` field were mapped
  41. 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. the following docs would *not* match the `missing` 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 match the `missing` filter:
  58. [source,js]
  59. --------------------------------------------------
  60. { "user": [] }
  61. { "foo": "bar" }
  62. --------------------------------------------------
  63. [float]
  64. ==== `existence` and `null_value` parameters
  65. When the field being queried has a `null_value` mapping, then the behaviour of
  66. the `missing` filter can be altered with the `existence` and `null_value`
  67. parameters:
  68. [source,js]
  69. --------------------------------------------------
  70. {
  71. "constant_score" : {
  72. "filter" : {
  73. "missing" : {
  74. "field" : "user",
  75. "existence" : true,
  76. "null_value" : false
  77. }
  78. }
  79. }
  80. }
  81. --------------------------------------------------
  82. `existence`::
  83. +
  84. --
  85. When the `existence` parameter is set to `true` (the default), the missing
  86. filter will include documents where the field has *no* values, ie:
  87. [source,js]
  88. --------------------------------------------------
  89. { "user": [] }
  90. { "foo": "bar" }
  91. --------------------------------------------------
  92. When set to `false`, these documents will not be included.
  93. --
  94. `null_value`::
  95. +
  96. --
  97. When the `null_value` parameter is set to `true`, the missing
  98. filter will include documents where the field contains a `null` value, ie:
  99. [source,js]
  100. --------------------------------------------------
  101. { "user": null }
  102. { "user": [null] }
  103. { "user": ["jane",null] } <1>
  104. --------------------------------------------------
  105. <1> Matches because the field contains a `null` value, even though it also contains a non-`null` value.
  106. When set to `false` (the default), these documents will not be included.
  107. --
  108. NOTE: Either `existence` or `null_value` or both must be set to `true`.