null-value.asciidoc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. [[null-value]]
  2. === `null_value`
  3. A `null` value cannot be indexed or searched. When a field is set to `null`,
  4. (or an empty array or an array of `null` values) it is treated as though that
  5. field has no values.
  6. The `null_value` parameter allows you to replace explicit `null` values with
  7. the specified value so that it can be indexed and searched. For instance:
  8. [source,js]
  9. --------------------------------------------------
  10. PUT my_index
  11. {
  12. "mappings": {
  13. "my_type": {
  14. "properties": {
  15. "status_code": {
  16. "type": "string",
  17. "index": "not_analyzed",
  18. "null_value": "NULL" <1>
  19. }
  20. }
  21. }
  22. }
  23. }
  24. PUT my_index/my_type/1
  25. {
  26. "status_code": null
  27. }
  28. PUT my_index/my_type/2
  29. {
  30. "status_code": [] <2>
  31. }
  32. GET my_index/_search
  33. {
  34. "query": {
  35. "term": {
  36. "status_code": "NULL" <3>
  37. }
  38. }
  39. }
  40. --------------------------------------------------
  41. // AUTOSENSE
  42. <1> Replace explicit `null` values with the term `NULL`.
  43. <2> An empty array does not contain an explicit `null`, and so won't be replaced with the `null_value`.
  44. <3> A query for `NULL` returns document 1, but not document 2.
  45. IMPORTANT: The `null_value` needs to be the same datatype as the field. For
  46. instance, a `long` field cannot have a string `null_value`. String fields
  47. which are `analyzed` will also pass the `null_value` through the configured
  48. analyzer.