2015-08-06 11:24:29 -04:00
|
|
|
[[dynamic]]
|
|
|
|
=== `dynamic`
|
|
|
|
|
|
|
|
By default, fields can be added _dynamically_ to a document, or to
|
|
|
|
<<object,inner objects>> within a document, just by indexing a document
|
|
|
|
containing the new field. For instance:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1 <1>
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"username": "johnsmith",
|
|
|
|
"name": {
|
|
|
|
"first": "John",
|
|
|
|
"last": "Smith"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
GET my_index/_mapping <2>
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/2 <3>
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"username": "marywhite",
|
|
|
|
"email": "mary@white.com",
|
|
|
|
"name": {
|
|
|
|
"first": "Mary",
|
|
|
|
"middle": "Alice",
|
|
|
|
"last": "White"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
GET my_index/_mapping <4>
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-04-29 10:42:03 -04:00
|
|
|
<1> This document introduces the string field `username`, the object field
|
2015-08-06 11:24:29 -04:00
|
|
|
`name`, and two string fields under the `name` object which can be
|
|
|
|
referred to as `name.first` and `name.last`.
|
2016-04-29 10:42:03 -04:00
|
|
|
<2> Check the mapping to verify the above.
|
|
|
|
<3> This document adds two string fields: `email` and `name.middle`.
|
|
|
|
<4> Check the mapping to verify the changes.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
The details of how new fields are detected and added to the mapping is explained in <<dynamic-mapping>>.
|
|
|
|
|
|
|
|
The `dynamic` setting controls whether new fields can be added dynamically or
|
|
|
|
not. It accepts three settings:
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`true`:: Newly detected fields are added to the mapping. (default)
|
2017-07-21 07:24:38 -04:00
|
|
|
`false`:: Newly detected fields are ignored. These fields will not be indexed so will not be searchable
|
|
|
|
but will still appear in the `_source` field of returned hits. These fields will not be added
|
|
|
|
to the mapping, new fields must be added explicitly.
|
|
|
|
`strict`:: If new fields are detected, an exception is thrown and the document is rejected. New fields
|
|
|
|
must be explicitly added to the mapping.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
The `dynamic` setting may be set at the mapping type level, and on each
|
|
|
|
<<object,inner object>>. Inner objects inherit the setting from their parent
|
|
|
|
object or from the mapping type. For instance:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2019-01-14 16:08:01 -05:00
|
|
|
PUT my_index?include_type_name=true
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc": {
|
2015-08-06 11:24:29 -04:00
|
|
|
"dynamic": false, <1>
|
|
|
|
"properties": {
|
|
|
|
"user": { <2>
|
|
|
|
"properties": {
|
|
|
|
"name": {
|
2016-03-18 12:01:27 -04:00
|
|
|
"type": "text"
|
2015-08-06 11:24:29 -04:00
|
|
|
},
|
|
|
|
"social_networks": { <3>
|
|
|
|
"dynamic": true,
|
|
|
|
"properties": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-06 11:24:29 -04:00
|
|
|
<1> Dynamic mapping is disabled at the type level, so no new top-level fields will be added dynamically.
|
|
|
|
<2> The `user` object inherits the type-level setting.
|
|
|
|
<3> The `user.social_networks` object enables dynamic mapping, so new fields may be added to this inner object.
|
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
TIP: The `dynamic` setting can be updated on existing fields
|
2015-08-12 15:21:37 -04:00
|
|
|
using the <<indices-put-mapping,PUT mapping API>>.
|