null-value.asciidoc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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,console]
  9. --------------------------------------------------
  10. PUT my_index
  11. {
  12. "mappings": {
  13. "properties": {
  14. "status_code": {
  15. "type": "keyword",
  16. "null_value": "NULL" <1>
  17. }
  18. }
  19. }
  20. }
  21. PUT my_index/_doc/1
  22. {
  23. "status_code": null
  24. }
  25. PUT my_index/_doc/2
  26. {
  27. "status_code": [] <2>
  28. }
  29. GET my_index/_search
  30. {
  31. "query": {
  32. "term": {
  33. "status_code": "NULL" <3>
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. <1> Replace explicit `null` values with the term `NULL`.
  39. <2> An empty array does not contain an explicit `null`, and so won't be replaced with the `null_value`.
  40. <3> A query for `NULL` returns document 1, but not document 2.
  41. IMPORTANT: The `null_value` needs to be the same datatype as the field. For
  42. instance, a `long` field cannot have a string `null_value`.
  43. NOTE: The `null_value` only influences how data is indexed, it doesn't modify
  44. the `_source` document.