Using dynamic templates in document mapping
In the Using explicit mapping creation recipe, we saw how ElasticSearch is able to guess the field type using reflection. In this recipe, we'll see how we can help it to improve its guessing capabilities via dynamic templates.
The dynamic template feature is very useful, for example, if you need to create several indices with similar types, because it allows you to remove the need to define mappings from coded initial routines to automatic index document creation. A typical use is to define types for logstash log indices.
Getting ready
You need a working ElasticSearch cluster.
How to do it...
You can extend the previous mapping by adding document-related settings:
{ "order" : { "index_analyzer":"standard", "search_analyzer":"standard", "dynamic_date_formats":["yyyy-MM-dd", "dd-MM-yyyy"], "date_detection":true, "numeric_detection":true, "dynamic_templates":[ {"template1":{ "match":"*", "match_mapping_type":"long", "mapping":{"type":" {dynamic_type}", "store":true} }} ], "properties" : {…} } }
How it works...
The Root object (document) controls the behavior of its fields and all its child object fields.
In the document mapping, you can define the following fields:
index_analyzer
: This defines the analyzer to be used for indexing within this document. If anindex_analyzer
field is not defined in a field, then the field is taken as the default.search_analyzer
: This defines the analyzer to be used for searching. If a field doesn't define an analyzer, thesearch_analyzer
field of the document, if available, is taken.date_detection
(by defaulttrue
): This enables the extraction of a date from a string.dynamic_date_formats
: This is a list of valid date formats; it's used ifdate_detection
is active.numeric_detection
(by defaultfalse
): This enables you to convert strings to numbers, if it is possible.dynamic_templates
: This is a list of the templates used to change the explicit mapping, if one of these templates is matched. The rules defined in it are used to build the final mapping.
A dynamic template is composed of two parts: the matcher and the mapping.
In order to match a field to activate the template, several types of matchers are available:
match
: This allows you to define a match on the field name. The expression is a standard glob pattern (http://en.wikipedia.org/wiki/Glob_(programming).unmatch
(optional): This allows you to define the expression to be used to exclude matches.match_mapping_type
(optional): This controls the types of the matched fields. For example, string, integer, and so on.path_match
(optional): This allows you to match the dynamic template against the full dot notation of the field. For example,obj1.*.value
.path_unmatch
(optional): This does the opposite ofpath_match
, that is, excluding the matched fields.match_pattern
(optional): This allows you to switch the matchers toregex
(regular expression); otherwise, the glob pattern match is used.
The dynamic template mapping part is standard, but with the ability to use special placeholders as follows:
{name}
:This will be replaced with the actual dynamic field name{dynamic_type}:
This will be replaced with the type of the matched field
There's more...
The dynamic template is very handy when you need to set a mapping configuration for all the fields. This action can be performed by adding a dynamic template similar to this one:
"dynamic_templates" : [ { "store_generic" : { "match" : "*", "mapping" : { "store" : "yes" } } } ]
In this example, all the new fields, which will be added with the explicit mapping, will be stored.
See also
- The Using explicit mapping creation recipe in this chapter
- The Mapping a document recipe in this chapter
- The Glob pattern at http://en.wikipedia.org/wiki/Glob_pattern