2015-08-06 11:24:29 -04:00
|
|
|
[[dynamic-field-mapping]]
|
|
|
|
=== Dynamic field mapping
|
|
|
|
|
|
|
|
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,`object`>> level, by
|
2017-01-05 14:36:18 -05:00
|
|
|
setting the <<dynamic,`dynamic`>> parameter to `false` (to ignore new fields) or to `strict` (to throw
|
|
|
|
an exception if an unknown field is encountered).
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
Assuming `dynamic` field mapping is enabled, some simple rules are used to
|
2020-07-07 14:59:35 -04:00
|
|
|
determine which data type the field should have:
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
[horizontal]
|
2020-07-07 14:59:35 -04:00
|
|
|
*JSON data type*:: *Elasticsearch data type*
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
`null`:: No field is added.
|
|
|
|
`true` or `false`:: <<boolean,`boolean`>> field
|
2016-05-07 11:16:31 -04:00
|
|
|
floating{nbsp}point{nbsp}number:: <<number,`float`>> field
|
2015-08-06 11:24:29 -04:00
|
|
|
integer:: <<number,`long`>> field
|
|
|
|
object:: <<object,`object`>> field
|
|
|
|
array:: Depends on the first non-`null` value in the array.
|
|
|
|
string:: Either a <<date,`date`>> field
|
|
|
|
(if the value passes <<date-detection,date detection>>),
|
|
|
|
a <<number,`double`>> or <<number,`long`>> field
|
|
|
|
(if the value passes <<numeric-detection,numeric detection>>)
|
2016-05-07 11:16:31 -04:00
|
|
|
or a <<text,`text`>> field, with a <<keyword,`keyword`>> sub-field.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2020-07-07 14:59:35 -04:00
|
|
|
These are the only <<mapping-types,field data types>> that are dynamically
|
|
|
|
detected. All other data types must be mapped explicitly.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
Besides the options listed below, dynamic field mapping rules can be further
|
|
|
|
customised with <<dynamic-templates,`dynamic_templates`>>.
|
|
|
|
|
|
|
|
[[date-detection]]
|
|
|
|
==== Date detection
|
|
|
|
|
|
|
|
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,`date`>> field is
|
|
|
|
added with the corresponding format.
|
|
|
|
|
|
|
|
The default value for `dynamic_date_formats` is:
|
|
|
|
|
|
|
|
[ <<strict-date-time,`"strict_date_optional_time"`>>,`"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`]
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"create_date": "2015/09/02"
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
GET my-index-000001/_mapping <1>
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
<1> The `create_date` field has been added as a <<date,`date`>>
|
|
|
|
field with the <<mapping-date-format,`format`>>: +
|
|
|
|
`"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`.
|
|
|
|
|
|
|
|
===== Disabling date detection
|
|
|
|
|
2016-04-15 03:22:06 -04:00
|
|
|
Dynamic date detection can be disabled by setting `date_detection` to `false`:
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"date_detection": false
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1 <1>
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"create": "2015/09/02"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2016-03-18 12:01:27 -04:00
|
|
|
<1> The `create_date` field has been added as a <<text,`text`>> field.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
===== Customising detected date formats
|
|
|
|
|
|
|
|
Alternatively, the `dynamic_date_formats` can be customised to support your
|
|
|
|
own <<mapping-date-format,date formats>>:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"dynamic_date_formats": ["MM/dd/yyyy"]
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"create_date": "09/25/2015"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
[[numeric-detection]]
|
|
|
|
==== Numeric detection
|
|
|
|
|
2020-07-07 14:59:35 -04:00
|
|
|
While JSON has support for native floating point and integer data types, some
|
2015-08-06 11:24:29 -04:00
|
|
|
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:
|
|
|
|
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"numeric_detection": true
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"my_float": "1.0", <1>
|
|
|
|
"my_integer": "1" <2>
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2018-06-05 02:51:52 -04:00
|
|
|
<1> The `my_float` field is added as a <<number,`float`>> field.
|
2015-08-06 11:24:29 -04:00
|
|
|
<2> The `my_integer` field is added as a <<number,`long`>> field.
|
|
|
|
|