Dynamic templates

When you create an index, Elasticsearch lets you create templates for your mapping rules to augment the newly added fields with the supported mapping types. A mapping rule contains two parts: the matcher and the corresponding mapping datatype. The matcher can be performed on one of the following:

  • The data type of the field: Using match_mapping_type for the datatype
  • The name of the field: Using match pattern and/or unmatch pattern for the field name
  • The full dotted path of the field: Using path_match and path_unmatch for the dotted path

Basically, the syntax of the dynamic templates is as follows:

"mappings":{
"dynamic_templates" :[
{
"template_name_1": {
...mapping rules...,
"mapping": {...}
}
},
{
"template_name_2":{...},
},...
]
}

Let's reuse the same index name, default_mappings_index, to create an example to show the dynamic template. The steps to be observed are as follows:

  1. Remove default_mappings_index, and recreate the same index with a dynamic template for the mapping
  2. Index the same sample document and examine the mapping types again
  3. Create three dynamic templates:
    • integers_template maps integer values to the integer type instead of the long type
    • field_name_template matches any fields to the name ending with _simple_string, and maps to the keyword type instead of the text type
    • path_match_template matches any fields with the dot path json_object.*.level_2_3, and maps to the double type instead of the float type:

 

  1. Once the sample document is indexed, the mapping of default_mappings_index will be as shown in the following screenshot: 

 

We can see that array_of_integer is mapped in the integer type, array_of_simple_string in the keyword type, and json_object.level_1.level_2_3 in the double type, since those fields match the criteria in the dynamic template.

It's also worth mentioning that we can use the placeholders {name} and {dynamic_type} in the template. {name} and {dynamic_type} represent the field name and the datatype detected, respectively. In the next section, we'll look at the meta field, which defines the metadata for a document, similar to the data structure for a database table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.155.130