field-mapping.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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",frame=all]
  15. |===
  16. h| 2+^h|{es} data type
  17. h| JSON data type h| `"dynamic":"true"` h| `"dynamic":"runtime"`
  18. |`null` 2*| No field added
  19. |`true` or `false` 2*| `boolean`
  20. |`double` | `float` | `double`
  21. |`long` 2*| `long`
  22. |`object` | `object` | No field added
  23. |`array` 2*| Depends on the first non-`null` value in the array
  24. |`string` that passes <<date-detection,date detection>> 2*| `date`
  25. |`string` that passes <<numeric-detection,numeric detection>> | `float` or `long` | `double` or `long`
  26. |`string` that doesn't pass `date` detection or `numeric` detection | `text` with a `.keyword` sub-field | `keyword`
  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_date": "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. [NOTE]
  92. ====
  93. There is a difference between configuring an array of date patterns and
  94. configuring multiple patterns in a single string separated by `||`. When you
  95. configure an array of date patterns, the pattern that matches the date in the
  96. first document with an unmapped date field will determine the mapping of that
  97. field:
  98. [source,console]
  99. --------------------------------------------------
  100. PUT my-index-000001
  101. {
  102. "mappings": {
  103. "dynamic_date_formats": [ "yyyy/MM", "MM/dd/yyyy"]
  104. }
  105. }
  106. PUT my-index-000001/_doc/1
  107. {
  108. "create_date": "09/25/2015"
  109. }
  110. --------------------------------------------------
  111. The resulting mapping will be:
  112. ////
  113. [source,console]
  114. ----
  115. GET my-index-000001/_mapping
  116. ----
  117. //TEST[continued]
  118. ////
  119. [source,console-result]
  120. --------------------------------------------------
  121. {
  122. "my-index-000001": {
  123. "mappings": {
  124. "dynamic_date_formats": [
  125. "yyyy/MM",
  126. "MM/dd/yyyy"
  127. ],
  128. "properties": {
  129. "create_date": {
  130. "type": "date",
  131. "format": "MM/dd/yyyy"
  132. }
  133. }
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. Configuring multiple patterns in a single string separated by `||` results in a
  139. mapping that supports any of the date formats. This enables you to index
  140. documents that use different formats:
  141. [source,console]
  142. --------------------------------------------------
  143. PUT my-index-000001
  144. {
  145. "mappings": {
  146. "dynamic_date_formats": [ "yyyy/MM||MM/dd/yyyy"]
  147. }
  148. }
  149. PUT my-index-000001/_doc/1
  150. {
  151. "create_date": "09/25/2015"
  152. }
  153. --------------------------------------------------
  154. The resulting mapping will be:
  155. ////
  156. [source,console]
  157. ----
  158. GET my-index-000001/_mapping
  159. ----
  160. //TEST[continued]
  161. ////
  162. [source,console-result]
  163. --------------------------------------------------
  164. {
  165. "my-index-000001": {
  166. "mappings": {
  167. "dynamic_date_formats": [
  168. "yyyy/MM||MM/dd/yyyy"
  169. ],
  170. "properties": {
  171. "create_date": {
  172. "type": "date",
  173. "format": "yyyy/MM||MM/dd/yyyy"
  174. }
  175. }
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. ====
  181. [NOTE]
  182. ====
  183. Epoch formats (`epoch_millis` and `epoch_second`) are not supported as dynamic date formats.
  184. ====
  185. [[numeric-detection]]
  186. ==== Numeric detection
  187. While JSON has support for native floating point and integer data types, some
  188. applications or languages may sometimes render numbers as strings. Usually the
  189. correct solution is to map these fields explicitly, but numeric detection
  190. (which is disabled by default) can be enabled to do this automatically:
  191. [source,console]
  192. --------------------------------------------------
  193. PUT my-index-000001
  194. {
  195. "mappings": {
  196. "numeric_detection": true
  197. }
  198. }
  199. PUT my-index-000001/_doc/1
  200. {
  201. "my_float": "1.0", <1>
  202. "my_integer": "1" <2>
  203. }
  204. --------------------------------------------------
  205. <1> The `my_float` field is added as a <<number,`float`>> field.
  206. <2> The `my_integer` field is added as a <<number,`long`>> field.