elasticsearch dynamic mapping

https://www.elastic.co/guide/cn/elasticsearch/guide/current/dynamic-mapping.html#dynamic-mapping
when Elasticsearch encounter fields that are not encountered in previous documents, it uses dynamic mapping to determine the field of data type and automatically adds a new field to the type mapping.
Sometimes this is the desired behavior sometimes do not want this. Usually no one knows what the future will have a new field to the document, but it is hoped that these fields are automatically indexed. Maybe you just want to ignore them. If Elasticsearch as an important data storage, expectations might encounter new field will throw an exception, so can find the problem.
Fortunately, you can use dynamic configuration to control this behavior, acceptable options are as follows:
to true dynamic add a new field - default
false ignore the new fields, field mapping is not added, but will be present in the _source in
strict if encounter new field thrown
configure dynamic parameters can be used on the root object or any object type of field. You can change the default value of the dynamic is set to strict, but only open it within the specified object, for example:

PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic":      "strict",
            "properties": {
                "title":  { "type": "string"},
                "stash":  {
                    "type":     "object",
                    "dynamic":  true 
                }
            }
        }
    }
}


If you encounter a new field, the object my_type will throw an exception. The internal object stash meet new fields will create a new dynamic field.
Using the dynamic mapping, you can add new objects to stash Searchable fields:

PUT /my_index/my_type/1
{
    "title":   "This doc adds a new field",
    "stash": { "new_field": "Success!" }
}

But the root objects my_type the same operation would fail:

PUT /my_index/my_type/1
{
    "title":     "This throws a StrictDynamicMappingException",
    "new_field": "Fail!"
}

The dynamic is set to false bit field will not change the content of _source. _source still contains the entire JSON document is indexed. But the new field will not be added to the map can not be searched.

Default mapping
Typically, an index of all types share the same field and set. _default_ maps more easily specify generic settings, rather than each time you create a new type setting must be repeated. _default_ mapping is a new type of template. After setting all types _default_ map created will apply the default settings, unless their type in the mapping explicitly override these settings.
For example, we can use the mapping for all types _default_ disabled _all field, but only enabled blog type:

PUT /my_index
{
    "mappings": {
        "_default_": {
            "_all": { "enabled":  false }
        },
        "blog": {
            "_all": { "enabled":  true  }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/gavinYang/p/11200205.html