# How to auto-generate default field when indexing?

**URL:** <https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140>\
**Category:** Elasticsearch\
**Created:** [May 27, 2016, 9:43am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140 "2016-05-27T09:43:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [May 27, 2016, 9:43am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/1 "2016-05-27T09:43:03Z")

</div>

When using bulk api to import data, some data were not neat.  
For example

```auto
{"name": "John", "gender": "male"}
{"name": "James"}
{"name": "Cindy", "gender": "female"}

```

The second row lacks field `gender`. But I hope es could auto-generate a `gender` field with a default value.  
In other words, I hope all the data with the same fields.  
How can I make it?  
Thank you!

---

<div class="post-metadata">

**Author:** ![Andreea\_Hazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreea_hazi/32/9994_2.png) [@Andreea\_Hazi](https://discuss.elastic.co/u/Andreea_Hazi)\
**Post date:** [May 27, 2016, 11:52am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/2 "2016-05-27T11:52:25Z")

</div>

You can make a very quick query directly from the browser to check if your record has this gender field:

[http://eshost:9200/index/type/\_search?q=name:james](http://eshost:9200/index/type/_search?q=name:james)

---

<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:** [May 28, 2016, 1:09am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/3 "2016-05-28T01:09:06Z")

</div>

This might also be useful [https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html)

---

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [May 30, 2016, 1:40am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/4 "2016-05-30T01:40:55Z")

</div>

Sorry, maybe you misunderstand my question?

I wanna know how to auto generate or pre-define one field.

If I index `{"name": "James"}`, it would turn be `{"name": "James", "gender": "default\_value"} in es.

That's to say there was no gender in index data, the gender field was auto-generated or pre-defined.

```auto
PUT test
{
  "mappings": {
    "sample": {
      "properties": {
        "gender": {
          "type": "string",
          "null_value": "male" 
        }
      }
    }
  }
}

PUT test/sample/2
{"name": "James"}

GET test/sample/_search

# the result was
{
    "_index": "test",
    "_type": "sample",
    "_id": "2",
        "_score": 1,
        "_source": {
          "name": "James"
        }
}

# but what I need is 
{
    "_index": "test",
    "_type": "sample",
    "_id": "2",
        "_score": 1,
        "_source": {
          "name": "James"
          "gender": "male" # auto-generated
        }
}

```

---

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [May 30, 2016, 1:44am UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/5 "2016-05-30T01:44:28Z")

</div>

Thank you, @warkolm

But I suppose it was only default value for null when searching, not default field when indexing.

Please see the code above.

---

<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:** [July 5, 2017, 10:48pm UTC](https://discuss.elastic.co/t/how-to-auto-generate-default-field-when-indexing/51140/6 "2017-07-05T22:48:14Z")

</div>


