Updating a mapping

Mappings for new fields can be added after a type has been created. A mapping can be updated for a type with the PUT mapping API. Let's add a code field, which is of the keyword type, but with no analysis:

PUT /catalog/_mapping
{
"properties": {
"code": {
"type": "keyword"
}
}
}

This mapping is merged into the existing mappings of the _doc type. The mapping looks like the following after it is merged:

{
"catalog" : {
"mappings" : {
"properties" : {
"code" : {
"type" : "keyword"
},
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text"
}
}
}
}
}

Any subsequent documents that are indexed with the code field are assigned the right datatype:

POST /catalog/_doc
{
"name": "sports",
"code": "C004",
"description": "Sports equipment"
}

This is how we can take control of the index creation and type mapping process, and add fields after the type is created.