strict: If a strange field is encountered, an exception is thrown.
dynamicThe setting can be applied to the root object orobjectAny field of the type. You should setdynamicSetstrictBut enable it for a specific internal object:
PUT /my_index{ "mappings": { "my_type": { "dynamic": "strict", "properties": { "title": { "type": "string"}, "stash": { "type": "object", "dynamic": true } } } }}
Inmy_typeIf an unknown field is encountered on the object, an exception is thrown. InstashNew fields are dynamically added to the object.
With the above ing, you canstashAdd a new searchable field:
PUT /my_index/my_type/1{ "title": "This doc adds a new field", "stash": { "new_field": "Success!" }}
However, if you try to add a new field to the top-level object, it will fail:
PUT /my_index/my_type/1{ "title": "This throws a StrictDynamicMappingException", "new_field": "Fail!"}
NOTE
SetdynamicSetfalseIt will not change_sourceField content-_sourceThe field will still save the entire JSON document you indexed. A strange field will not be added to the ing, so that it cannot be searched.
Custom dynamic ing
If you know that you need to dynamically add new fields, you may enable dynamic ing. However, sometimes the dynamic ing rules are not flexible enough. Fortunately, you can adjust some settings to make dynamic ing rules more suitable for your data.
date_detection
When ES encounters a new string field, it checks whether the string contains a recognizable date, such2014-01-01. If yes, it will be recognized asdateType field. Otherwise, it will be usedstring.
Sometimes this behavior may cause some problems. If you want to index a document like this:
{ "note": "2014-01-01" }
Suppose this isnoteWhen a field is first discovered, it will be useddateField. However, if the next document is as follows:
{ "note": "Logged out" }
This field is obviously not a date, but it is too late. This field is already of the date type. Therefore, an exception is thrown.
You candate_detectionSetfalseTo disable date Detection:
PUT /my_index{ "mappings": { "my_type": { "date_detection": false } }}
With the above ing, a string will always be treatedstringType. If you needdateField, You need to manually add it.
NOTE
Elasticsearch supports date recognition throughdynamic_date_formatsSet Change.
dynamic_templates
Passdynamic_templatesYou can have full control over the dynamic ing rules of new fields. You can use a different ing rule based on the field name or type.
Each template has a name that describes what the template has done. At the same time, it hasmappingSpecifies the specific ing information and at least one parameter (for examplematchSpecifies the fields to use this template.
Template matching is ordered-the first matching template will be used. For examplestringThe field specifies two templates:
es:_esThe end field should bespanishParser
en: Use of all other fieldsenglishParser
We needesThe template is placed in the first one, because it is betterenThe template is more specific:
PUT /my_index{ "mappings": { "my_type": { "dynamic_templates": [ { "es": { "match": "*_es", "match_mapping_type": "string", "mapping": { "type": "string", "analyzer": "spanish" } }}, { "en": { "match": "*", "match_mapping_type": "string", "mapping": { "type": "string", "analyzer": "english" } }} ]}}}
match_mapping_typeAllows you to use templates only for specific fields, as in standard dynamic ing rules, suchstring,long.
matchThe parameter only matches the field name,path_matchParameters are used to match the complete path of fields in the object, suchaddress.*.nameThe following fields can be matched:
{ "address": "city": "name": "New York" } }}
unmatchAndpath_unmatchMode can be used to exclude certain fields. fields that are not excluded will be matched.
More configuration options can be found in the root object reference document.