IV. Dynamic Mapping
4.1 default mapping
If the default field is used in mapping, the other fields automatically inherit the settings from default.
PUT http://node1:9200/my_index { "mappings":{ "_default_":{ "_all":{ "enable":false } }, "user":{ }, "blogpost":{ "_all":{ "enable":true } } } }
The _all field in default is obsolete at 5.x, so the exception above will occur but the rule is available.
In the mapping above, the all field is turned off in default, and the user inherits the configuration in _default, so the all field in user is also turned off, and _all is turned on in blogpost, overriding the default configuration in _default.
When default is updated, it will only work on documents that are added later.
4.2 Dynamic field mapping
When a field in the document that has not previously appeared is added to ELasticsearch, a new field is automatically added to the document's type mapping.This can be controlled by the dynamic property, which ignores new fields and throws exceptions if the dynamic property is string.If dynamic is true, ELasticsearch automatically infers the type based on the value of the field and determines mapping:
Data in JSON format | Auto-guessed field type |
---|---|
null | No fields were added |
true or false | boolean type |
floating type number | floating type |
integer | long type |
JSON Object | object type |
array | Determined by the first non-null value in the array |
string | Possibly date type (open date detection), double or long type, text type, keyword type |
Date detection defaults to detecting strings that conform to the following date formats:
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
Example:
POST http://node1:9200/my_index/my_type/1 { "create_date": "2015/09/02" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "properties": { "create_date": { "type": "date", "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis" } } } } } }
Turn off date detection:
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "date_detection":false } } } POST http://node1:9200/my_index/my_type/1 { "create_date": "2015/09/02" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "date_detection": false, "properties": { "create_date": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } } }
Custom date detection format:
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "dynamic_date_formats":["MM/dd/yyyy"] } } } POST http://node1:9200/my_index/my_type/1 { "create_date": "09/25/2015" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "dynamic_date_formats": [ "MM/dd/yyyy" ], "properties": { "create_date": { "type": "date", "format": "MM/dd/yyyy" } } } } } }
Turn on automatic number type detection:
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "numeric_detection":true } } } POST http://node1:9200/my_index/my_type/1 { "my_float": "1.0", "my_integer": "1" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "numeric_detection": true, "properties": { "my_float": { "type": "float" }, "my_integer": { "type": "long" } } } } } }
4.3 Dynamic templates
Dynamic templates can set mapping based on field names, as follows for string-type fields, set mapping to:
"mapping": { "type": "long"}
However, if the matching field name is in long_* format, it does not match that in *_text format:
PUT http://node1:9200/my_index { "mappings": { "my_type": { "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] } } } PUT http://node1:9200/my_index/my_type/1 { "long_num": "5", "long_text": "foo" }
When the document is written, the long_num field is of type long and the long_text is discarded as type string.
4.4 Override default template
You can override the mapping configuration of all indexes through the default field, for example:
PUT _template/disable_all_field { "order": 0, "template": "*", "mappings": { "_default_": { "_all": { "enabled": false } } } }