Dynamic templates allow to define mapping templates that will be applied when dynamic introduction of fields / objects happens.
For example, we might want to have all fields to be stored by default, or all string fields to be stored, or have string fields to always be indexed as multi_field once analyzed and once not_analyzed. Here is a simple example:
{
"person" : {
"dynamic_templates" : [
{
"template_1" : {
"match" : "multi*",
"mapping" : {
"type" : "multi_field",
"fields" : {
"{name}" : {"type": "{dynamic_tpye}", "index" : "analyzed", "store" : "yes"},
"org" : {"type": "{dynamic_type}", "index" : "not_analyzed", "store" : "yes"}
}
}
}
},
{
"template_2" : {
"match" : "*",
"match_mapping_type" : "string",
"mapping" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
]
}
}
The above mapping will create a multi_field mapping for all field names starting with multi, and will map all string types to be not_analyzed.
The dynamic_templates section can be placed only on the root object. and it is applied to all inner objects / fields.
Dynamic templates are named to allow for simple merge behavior. A new mapping, just with a new template can be "put" and that template will be added, or if it has the same name, the template will be replaced.
The match allow to define matching on the field name. An unmatch option is also available to exclude fields if they do match on match. The match_mapping_type controls if this template will be applied only for dynamic fields of the specified type (as guessed by the json format).
The format of all the matching is simple format, allowing to use * as a matching element supporting simple patterns such as xxx*, *xxx, xxx*yyy (with arbitrary number of pattern types), as well as direct equality. The match_pattern can be set to regex to allow for regular expression based matching.
The mapping element provides the actual mapping definition. The {name} keyword can be used and will be replaced with the actual dynamic field name being introduced. The {dynamic_type} (or {dynamicType}) can be used and will be replaced with the mapping derived based on the field type (or the derived type, like date).
Complete generic settings can also be applied, for example, to have all mappings be stored, just set:
{
"person" : {
"dynamic_templates" : [
{
"store_generic" : {
"match" : "*",
"mapping" : {
"store" : "yes"
}
}
}
]
}
}
Dynamic templates allow to define mapping templates that will be applied when dynamic introduction of fields / objects happens.
For example, we might want to have all fields to be stored by default, or all
stringfields to be stored, or havestringfields to always be indexed asmulti_fieldonceanalyzedand oncenot_analyzed. Here is a simple example:The above mapping will create a
multi_fieldmapping for all field names starting withmulti, and will map allstringtypes to benot_analyzed.The
dynamic_templatessection can be placed only on the root object. and it is applied to all inner objects / fields.Dynamic templates are named to allow for simple merge behavior. A new mapping, just with a new template can be "put" and that template will be added, or if it has the same name, the template will be replaced.
The
matchallow to define matching on the field name. Anunmatchoption is also available to exclude fields if they do match onmatch. Thematch_mapping_typecontrols if this template will be applied only for dynamic fields of the specified type (as guessed by the json format).The format of all the matching is
simpleformat, allowing to use*as a matching element supporting simple patterns such asxxx*,*xxx,xxx*yyy(with arbitrary number of pattern types), as well as direct equality. Thematch_patterncan be set toregexto allow for regular expression based matching.The
mappingelement provides the actual mapping definition. The{name}keyword can be used and will be replaced with the actual dynamic field name being introduced. The{dynamic_type}(or{dynamicType}) can be used and will be replaced with the mapping derived based on the field type (or the derived type, likedate).Complete generic settings can also be applied, for example, to have all mappings be stored, just set: