| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | [[dynamic-field-mapping]]=== Dynamic field mappingBy default, when a previously unseen field is found in a document,Elasticsearch will add the new field to the type mapping.   This behaviour canbe disabled, both at the document and at the <<object,`object`>>  level, bysetting the <<dynamic,`dynamic`>> parameter to `false` (to ignore new fields) or to `strict` (to throwan exception if an unknown field is encountered).Assuming `dynamic` field mapping is enabled, some simple rules are used todetermine which datatype the field should have:[horizontal]*JSON datatype*::                   *Elasticsearch datatype*`null`::                            No field is added.`true` or `false`::                 <<boolean,`boolean`>> fieldfloating{nbsp}point{nbsp}number::   <<number,`float`>> fieldinteger::                           <<number,`long`>> fieldobject::                            <<object,`object`>> fieldarray::                             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>>)                                    or a <<text,`text`>> field, with a <<keyword,`keyword`>> sub-field.These are the only <<mapping-types,field datatypes>> that are dynamicallydetected.  All other datatypes must be mapped explicitly.Besides the options listed below, dynamic field mapping rules can be furthercustomised with <<dynamic-templates,`dynamic_templates`>>.[[date-detection]]==== Date detectionIf `date_detection` is enabled (default), then new string fields are checkedto 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 isadded 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:[source,console]--------------------------------------------------PUT my_index/_doc/1{  "create_date": "2015/09/02"}GET my_index/_mapping <1>--------------------------------------------------<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 detectionDynamic date detection can be disabled by setting `date_detection` to `false`:[source,console]--------------------------------------------------PUT my_index{  "mappings": {    "date_detection": false  }}PUT my_index/_doc/1 <1>{  "create": "2015/09/02"}--------------------------------------------------<1> The `create_date` field has been added as a <<text,`text`>> field.===== Customising detected date formatsAlternatively, the `dynamic_date_formats` can be customised to support yourown <<mapping-date-format,date formats>>:[source,console]--------------------------------------------------PUT my_index{  "mappings": {    "dynamic_date_formats": ["MM/dd/yyyy"]  }}PUT my_index/_doc/1{  "create_date": "09/25/2015"}--------------------------------------------------[[numeric-detection]]==== Numeric detectionWhile JSON has support for native floating point and integer datatypes, someapplications or languages may sometimes render numbers as strings. Usually thecorrect solution is to map these fields explicitly, but numeric detection(which is disabled by default) can be enabled to do this automatically:[source,console]--------------------------------------------------PUT my_index{  "mappings": {    "numeric_detection": true  }}PUT my_index/_doc/1{  "my_float":   "1.0", <1>  "my_integer": "1" <2>}--------------------------------------------------<1> The `my_float` field is added as a <<number,`float`>> field.<2> The `my_integer` field is added as a <<number,`long`>> field.
 |