|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Null value |
| 4 | +parent: Mapping parameters |
| 5 | +grand_parent: Mapping and field types |
| 6 | +nav_order: 130 |
| 7 | +has_children: false |
| 8 | +has_toc: false |
| 9 | +--- |
| 10 | + |
| 11 | +# Null value |
| 12 | + |
| 13 | +The `null_value` mapping parameter allows you to replace explicit `null` values with a predefined substitute during indexing. By default, if a field is set to `null`, it is not indexed and cannot be searched. With `null_value` defined, the specified replacement value is indexed instead. This allows you to query or aggregate documents in which a field was originally `null` without modifying the document `_source`. |
| 14 | + |
| 15 | +The `null_value` must be of the same type as the field it is applied to. For instance, a `date` field cannot use a `boolean` such as `true` as its `null_value`; the `null_value` must be a valid date string. |
| 16 | +{: .important} |
| 17 | + |
| 18 | +## Setting a null_value on a field |
| 19 | + |
| 20 | +The following request creates an index named `products`. The `category` field is of type `keyword` and replaces `null` values with `"unknown"` during indexing: |
| 21 | + |
| 22 | +```json |
| 23 | +PUT /products |
| 24 | +{ |
| 25 | + "mappings": { |
| 26 | + "properties": { |
| 27 | + "category": { |
| 28 | + "type": "keyword", |
| 29 | + "null_value": "unknown" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | +{% include copy-curl.html %} |
| 36 | + |
| 37 | +## Indexing a document with a null value |
| 38 | + |
| 39 | +Use the following command to index a document in which the `category` field is set to `null`: |
| 40 | + |
| 41 | +```json |
| 42 | +PUT /products/_doc/1 |
| 43 | +{ |
| 44 | + "category": null |
| 45 | +} |
| 46 | +``` |
| 47 | +{% include copy-curl.html %} |
| 48 | + |
| 49 | +## Querying the null substitute |
| 50 | + |
| 51 | +Use the following command to search for documents in which the `category` field was previously `null`: |
| 52 | + |
| 53 | +```json |
| 54 | +POST /products/_search |
| 55 | +{ |
| 56 | + "query": { |
| 57 | + "term": { |
| 58 | + "category": "unknown" |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | +{% include copy-curl.html %} |
| 64 | + |
| 65 | +The response contains the matching document: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + ... |
| 70 | + "hits": { |
| 71 | + "total": { |
| 72 | + "value": 1, |
| 73 | + "relation": "eq" |
| 74 | + }, |
| 75 | + "max_score": 0.2876821, |
| 76 | + "hits": [ |
| 77 | + { |
| 78 | + "_index": "products", |
| 79 | + "_id": "1", |
| 80 | + "_score": 0.2876821, |
| 81 | + "_source": { |
| 82 | + "category": null |
| 83 | + } |
| 84 | + } |
| 85 | + ] |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Aggregating on a null substitute |
| 91 | + |
| 92 | +Because the null replacement is indexed, it also appears in aggregations. Use the following command to perform a `terms` aggregation on the `category` field: |
| 93 | + |
| 94 | +```json |
| 95 | +POST /products/_search |
| 96 | +{ |
| 97 | + "size": 0, |
| 98 | + "aggs": { |
| 99 | + "category_count": { |
| 100 | + "terms": { |
| 101 | + "field": "category" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | +{% include copy-curl.html %} |
| 108 | + |
| 109 | +The response contains aggregated results: |
| 110 | + |
| 111 | +```json |
| 112 | +{ |
| 113 | + ... |
| 114 | + "hits": { |
| 115 | + "total": { |
| 116 | + "value": 1, |
| 117 | + "relation": "eq" |
| 118 | + }, |
| 119 | + "max_score": null, |
| 120 | + "hits": [] |
| 121 | + }, |
| 122 | + "aggregations": { |
| 123 | + "category_count": { |
| 124 | + "doc_count_error_upper_bound": 0, |
| 125 | + "sum_other_doc_count": 0, |
| 126 | + "buckets": [ |
| 127 | + { |
| 128 | + "key": "unknown", |
| 129 | + "doc_count": 1 |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments