原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/dynamic-field-mapping.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/dynamic-field-mapping.html

Dynamic field mappingedit

By default, when a previously unseen field is found in a document, Elasticsearch will add the new field to the type mapping. This behaviour can be disabled, both at the document and at the object level, by setting the dynamic parameter to false (to ignore new fields) or to strict (to throw an exception if an unknown field is encountered).

Assuming dynamic field mapping is enabled, some simple rules are used to determine which datatype the field should have:

JSON datatype

Elasticsearch datatype

null

No field is added.

true or false

boolean field

floating point number

float field

integer

long field

object

object field

array

Depends on the first non-null value in the array.

string

Either a date field (if the value passes date detection), a double or long field (if the value passes numeric detection) or a text field, with a keyword sub-field.

These are the only field datatypes that are dynamically detected. All other datatypes must be mapped explicitly.

Besides the options listed below, dynamic field mapping rules can be further customised with dynamic_templates.

Date detectionedit

If date_detection is enabled (default), then new string fields are checked to see whether their contents match any of the date patterns specified in dynamic_date_formats. If a match is found, a new date field is added with the corresponding format.

The default value for dynamic_date_formats is:

[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]

For example:

PUT my_index/_doc/1
{
  "create_date": "2015/09/02"
}

GET my_index/_mapping 

The create_date field has been added as a date field with the format:
"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z".

Disabling date detectionedit

Dynamic date detection can be disabled by setting date_detection to false:

PUT my_index
{
  "mappings": {
    "date_detection": false
  }
}

PUT my_index/_doc/1 
{
  "create": "2015/09/02"
}

The create_date field has been added as a text field.

Customising detected date formatsedit

Alternatively, the dynamic_date_formats can be customised to support your own date formats:

PUT my_index
{
  "mappings": {
    "dynamic_date_formats": ["MM/dd/yyyy"]
  }
}

PUT my_index/_doc/1
{
  "create_date": "09/25/2015"
}

Numeric detectionedit

While JSON has support for native floating point and integer datatypes, some applications or languages may sometimes render numbers as strings. Usually the correct solution is to map these fields explicitly, but numeric detection (which is disabled by default) can be enabled to do this automatically:

PUT my_index
{
  "mappings": {
    "numeric_detection": true
  }
}

PUT my_index/_doc/1
{
  "my_float":   "1.0", 
  "my_integer": "1" 
}

The my_float field is added as a float field.

The my_integer field is added as a long field.