How to auto-generate default field when indexing?

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

{"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!

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

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

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.

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
        }
}

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.