strict
: If a strange field is encountered, an exception is thrown.dynamic
The setting can be applied to the root object orobject
Any field of the type. You should setdynamic
Setstrict
But 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_type
If an unknown field is encountered on the object, an exception is thrown. Instash
New fields are dynamically added to the object.
With the above ing, you canstash
Add 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
Setdynamic
Setfalse
It will not change_source
Field content-_source
The 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 ingIf 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 asdate
Type 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 isnote
When a field is first discovered, it will be useddate
Field. 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_detection
Setfalse
To disable date Detection:
PUT /my_index{ "mappings": { "my_type": { "date_detection": false } }}
With the above ing, a string will always be treatedstring
Type. If you needdate
Field, You need to manually add it.
NOTE
Elasticsearch supports date recognition throughdynamic_date_formats
Set Change.
dynamic_templates
Passdynamic_templates
You 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 hasmapping
Specifies the specific ing information and at least one parameter (for examplematch
Specifies the fields to use this template.
Template matching is ordered-the first matching template will be used. For examplestring
The field specifies two templates:
es
:_es
The end field should bespanish
Parser
en
: Use of all other fieldsenglish
ParserWe needes
The template is placed in the first one, because it is betteren
The 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_type
Allows you to use templates only for specific fields, as in standard dynamic ing rules, suchstring
,long
.
match
The parameter only matches the field name,path_match
Parameters are used to match the complete path of fields in the object, suchaddress.*.name
The following fields can be matched:
{ "address": "city": "name": "New York" } }}
unmatch
Andpath_unmatch
Mode 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.