field-mapping.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. [[dynamic-field-mapping]]
  2. === Dynamic field mapping
  3. When {es} detects a new field in a document, it _dynamically_ adds the field to
  4. the type mapping by default. The <<dynamic,`dynamic`>> parameter controls this behavior.
  5. You can explicitly instruct {es} to dynamically create fields based on incoming
  6. documents by setting the `dynamic` parameter to `true` or `runtime`. When
  7. dynamic field mapping is enabled, {es} uses the rules in the following table to
  8. determine how to map data types for each field.
  9. NOTE: The field data types in the following table are the only
  10. <<mapping-types,field data types>> that {es} detects dynamically. You must
  11. explicitly map all other data types.
  12. [[dynamic-field-mapping-types]]
  13. // tag::dynamic-field-mapping-types-tag[]
  14. [cols="3"]
  15. |===
  16. h| JSON data type h| `"dynamic":"true"` h| `"dynamic":"runtime"`
  17. |`null` 2*| No field added
  18. |`true` or `false` 2*| `boolean`
  19. |`double` | `float` | `double`
  20. |`integer` 2*| `long`
  21. |`object`^1^ 2*| `object`
  22. |`array` 2*| Depends on the first non-`null` value in the array
  23. |`string` that passes <<date-detection,date detection>> 2*| `date`
  24. |`string` that passes <<numeric-detection,numeric detection>> | `float` or `long` | `double` or `long`
  25. |`string` that doesn't pass `date` detection or `numeric` detection | `text` with a `.keyword` sub-field | `keyword`
  26. 3+| ^1^Objects are always mapped as part of the `properties` section, even when the `dynamic` parameter is set to `runtime`. | |
  27. |===
  28. // end::dynamic-field-mapping-types-tag[]
  29. You can disable dynamic mapping, both at the document and at the
  30. <<object,`object`>> level. Setting the `dynamic` parameter to
  31. `false` ignores new fields, and `strict` rejects the document if {es}
  32. encounters an unknown field.
  33. TIP: Use the <<indices-put-mapping,update mapping API>> to update the `dynamic`
  34. setting on existing fields.
  35. You can customize dynamic field mapping rules for
  36. <<date-detection,date detection>> and <<numeric-detection,numeric detection>>.
  37. To define custom mappings rules that you can apply to additional dynamic
  38. fields, use <<dynamic-templates,`dynamic_templates`>>.
  39. [[date-detection]]
  40. ==== Date detection
  41. If `date_detection` is enabled (default), then new string fields are checked
  42. to see whether their contents match any of the date patterns specified in
  43. `dynamic_date_formats`. If a match is found, a new <<date,`date`>> field is
  44. added with the corresponding format.
  45. The default value for `dynamic_date_formats` is:
  46. &#91; <<strict-date-time,`"strict_date_optional_time"`>>,`"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`]
  47. For example:
  48. [source,console]
  49. --------------------------------------------------
  50. PUT my-index-000001/_doc/1
  51. {
  52. "create_date": "2015/09/02"
  53. }
  54. GET my-index-000001/_mapping <1>
  55. --------------------------------------------------
  56. <1> The `create_date` field has been added as a <<date,`date`>>
  57. field with the <<mapping-date-format,`format`>>: +
  58. `"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`.
  59. ===== Disabling date detection
  60. Dynamic date detection can be disabled by setting `date_detection` to `false`:
  61. [source,console]
  62. --------------------------------------------------
  63. PUT my-index-000001
  64. {
  65. "mappings": {
  66. "date_detection": false
  67. }
  68. }
  69. PUT my-index-000001/_doc/1 <1>
  70. {
  71. "create": "2015/09/02"
  72. }
  73. --------------------------------------------------
  74. <1> The `create_date` field has been added as a <<text,`text`>> field.
  75. ===== Customizing detected date formats
  76. Alternatively, the `dynamic_date_formats` can be customized to support your
  77. own <<mapping-date-format,date formats>>:
  78. [source,console]
  79. --------------------------------------------------
  80. PUT my-index-000001
  81. {
  82. "mappings": {
  83. "dynamic_date_formats": ["MM/dd/yyyy"]
  84. }
  85. }
  86. PUT my-index-000001/_doc/1
  87. {
  88. "create_date": "09/25/2015"
  89. }
  90. --------------------------------------------------
  91. [[numeric-detection]]
  92. ==== Numeric detection
  93. While JSON has support for native floating point and integer data types, some
  94. applications or languages may sometimes render numbers as strings. Usually the
  95. correct solution is to map these fields explicitly, but numeric detection
  96. (which is disabled by default) can be enabled to do this automatically:
  97. [source,console]
  98. --------------------------------------------------
  99. PUT my-index-000001
  100. {
  101. "mappings": {
  102. "numeric_detection": true
  103. }
  104. }
  105. PUT my-index-000001/_doc/1
  106. {
  107. "my_float": "1.0", <1>
  108. "my_integer": "1" <2>
  109. }
  110. --------------------------------------------------
  111. <1> The `my_float` field is added as a <<number,`float`>> field.
  112. <2> The `my_integer` field is added as a <<number,`long`>> field.