# Use special (null?) value for missing field

**URL:** <https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952>\
**Category:** Elasticsearch\
**Created:** [March 28, 2018, 2:43pm UTC](https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952 "2018-03-28T14:43:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [March 28, 2018, 2:43pm UTC](https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952/1 "2018-03-28T14:43:46Z")

</div>

I want to import some data into ES.

There is an integer field in my template and I want to use `strict` mapping.

However, there might be the case that in some documents, the specific filed is missing.

What I want to achieve is to have a special value, say `null` ( **not** the **string**"null" !) for the documents where the field is missing.

If I perform

```
  if !([my_field]) {
      mutate {
        add_field => ["[my_field]", null ]
      }
    }

```

... and the type is set to `integer` in my index template, it will convert it to '0' during import;

Is there a way to keep a special value for **missing** fields during import?

Or do I have to resort to just assigning a special pre-defined value by convention (e.g. `-999`)

---

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [March 29, 2018, 11:45am UTC](https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952/2 "2018-03-29T11:45:15Z")

</div>

I have managed to do this via the following inline ruby code:

```
if !([my_field]) {
  ruby {
    code => "event.set('my_field' , nil)"
  }

```

However the problem now is that the following query:

```
"query": {
    "match" : {
        "my_field" : null
    }
}

```

will fetch **ALL** documents and not only those whose specific field has the value `null` .

How can I prevent this behavior?

---

<div class="post-metadata">

**Author:** ![Sravanthi\_N\_S\_CH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sravanthi_n_s_ch/32/50473_2.png) [@Sravanthi\_N\_S\_CH](https://discuss.elastic.co/u/Sravanthi_N_S_CH)\
**Post date:** [March 31, 2018, 7:14am UTC](https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952/3 "2018-03-31T07:14:45Z")

</div>

If you are planning to fetch the fields that have missing field, then you could use the below query

```
"query": {
    "bool": {
        "must_not": {
            "exists": {
                "field": "my_field"
            }
        }
    }
}

```

Hope this helps ..!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 28, 2018, 7:15am UTC](https://discuss.elastic.co/t/use-special-null-value-for-missing-field/125952/4 "2018-04-28T07:15:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
