# Default values for missing fields

**URL:** <https://discuss.elastic.co/t/default-values-for-missing-fields/303333>\
**Category:** Elasticsearch\
**Created:** [April 26, 2022, 10:38pm UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333 "2022-04-26T22:38:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfinan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tfinan/32/100693_2.png) [@tfinan](https://discuss.elastic.co/u/tfinan)\
**Post date:** [April 26, 2022, 10:38pm UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/1 "2022-04-26T22:38:08Z")

</div>

Can I create a field in a mapping that will be indexed with some default value if that field is not present in the document being indexed?

In other words, if I have a mapping with the field {"Rating": "integer"}, and then I index a document that does not include this field, can I have the field "Rating" be indexed, for example, with a value of zero?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 26, 2022, 10:39pm UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/2 "2022-04-26T22:39:06Z")

</div>

Take a look at [null\_value | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/null-value.html)

---

<div class="post-metadata">

**Author:** ![tfinan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tfinan/32/100693_2.png) [@tfinan](https://discuss.elastic.co/u/tfinan)\
**Post date:** [April 27, 2022, 2:07am UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/3 "2022-04-27T02:07:53Z")

</div>

I don't think null\_value does what I want (unless I don't understand the documentation). Here is precisely what I want to do. I want to create an index with the following mapping:

```auto
PUT /testindex
{
  "mappings": {
    "properties": {
      "email": { "type": "keyword"}, 
      "name": { "type": "text"},     
      "age": { "type": "integer"}  
    }
  }
}

```

Then I want to index the following document:

```auto
PUT /testindex/_doc/1
{
  "name":"John Smith",
  "email":"jsmith@email.com"
}

```

[note that I only indexed two of the fields defined in my mapping]  
When I then retrieve that document like this:

```auto
GET /testindex/_doc/1

```

I am HOPING to get all three of my fields, but with the missing field ("age") set to some default value. But I only get the following response:

```auto
{
  "_index" : "testindex",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Smith",
    "email" : "jsmith@email.com"
  }
}

```

My reading of the null\_value parameter seems to indicate that it only applies when I _explicitly_ make one of the fields null.

Is there any way for me to force "age" to be set to a default value?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 27, 2022, 3:13am UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/4 "2022-04-27T03:13:45Z")

</div>

There's no other way to do that in Elasticsearch unfortunately.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [April 27, 2022, 4:45am UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/5 "2022-04-27T04:45:08Z")

</div>

> [@tfinan](#):
>
> Is there any way for me to force "age" to be set to a default value?

I think that the only way to do something like that is using an ingest pipeline with some processors that would add the field if it does not exist.

It is not the cleanest way as it depends on the number of fields you want to add, but it works. You could create an ingest pipeline, configure your template to use this pipeline as the `index.final_pipeline` and then add some `set` processors with conditionals to check if the field exists.

An example of a `set` processor would be this:

```auto
    {
      "set": {
        "field": "age",
        "value": 0,
        "override": false,
        "if": "ctx.age == null",
        "ignore_failure": true
      }
    }

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 27, 2022, 5:00am UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/6 "2022-04-27T05:00:34Z")

</div>

That's a great solution!

---

<div class="post-metadata">

**Author:** ![tfinan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tfinan/32/100693_2.png) [@tfinan](https://discuss.elastic.co/u/tfinan)\
**Post date:** [April 27, 2022, 1:47pm UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/7 "2022-04-27T13:47:00Z")

</div>

Thanks everyone, I appreciate the help.

---

<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:** [May 25, 2022, 1:47pm UTC](https://discuss.elastic.co/t/default-values-for-missing-fields/303333/8 "2022-05-25T13:47:13Z")

</div>

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