field-mapping.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. [[dynamic-field-mapping]]
  2. === Dynamic field mapping
  3. By default, when a previously unseen field is found in a document,
  4. Elasticsearch will add the new field to the type mapping. This behaviour can
  5. be disabled, both at the document and at the <<object,`object`>> level, by
  6. setting the <<dynamic,`dynamic`>> parameter to `false` or to `strict`.
  7. Assuming `dynamic` field mapping is enabled, some simple rules are used to
  8. determine which datatype the field should have:
  9. [horizontal]
  10. *JSON datatype*:: *Elasticsearch datatype*
  11. `null`:: No field is added.
  12. `true` or `false`:: <<boolean,`boolean`>> field
  13. floating{nbsp}point{nbsp}number:: <<number,`double`>> field
  14. integer:: <<number,`long`>> field
  15. object:: <<object,`object`>> field
  16. array:: Depends on the first non-`null` value in the array.
  17. string:: Either a <<date,`date`>> field
  18. (if the value passes <<date-detection,date detection>>),
  19. a <<number,`double`>> or <<number,`long`>> field
  20. (if the value passes <<numeric-detection,numeric detection>>)
  21. or an <<text,`text`>> field.
  22. These are the only <<mapping-types,field datatypes>> that are dynamically
  23. detected. All other datatypes must be mapped explicitly.
  24. Besides the options listed below, dynamic field mapping rules can be further
  25. customised with <<dynamic-templates,`dynamic_templates`>>.
  26. [[total-fields-limit]]
  27. ==== Total fields limit
  28. To avoid mapping explosion, Index has a default limit of 1000 total number of fields.
  29. The default setting can be updated with `index.mapping.total_fields.limit`.
  30. [[date-detection]]
  31. ==== Date detection
  32. If `date_detection` is enabled (default), then new string fields are checked
  33. to see whether their contents match any of the date patterns specified in
  34. `dynamic_date_formats`. If a match is found, a new <<date,`date`>> field is
  35. added with the corresponding format.
  36. The default value for `dynamic_date_formats` is:
  37. &#91; <<strict-date-time,`"strict_date_optional_time"`>>,`"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`]
  38. For example:
  39. [source,js]
  40. --------------------------------------------------
  41. PUT my_index/my_type/1
  42. {
  43. "create_date": "2015/09/02"
  44. }
  45. GET my_index/_mapping <1>
  46. --------------------------------------------------
  47. // AUTOSENSE
  48. <1> The `create_date` field has been added as a <<date,`date`>>
  49. field with the <<mapping-date-format,`format`>>: +
  50. `"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`.
  51. ===== Disabling date detection
  52. Dynamic date dection can be disabled by setting `date_detection` to `false`:
  53. [source,js]
  54. --------------------------------------------------
  55. PUT my_index
  56. {
  57. "mappings": {
  58. "my_type": {
  59. "date_detection": false
  60. }
  61. }
  62. }
  63. PUT my_index/my_type/1 <1>
  64. {
  65. "create": "2015/09/02"
  66. }
  67. --------------------------------------------------
  68. // AUTOSENSE
  69. <1> The `create_date` field has been added as a <<text,`text`>> field.
  70. ===== Customising detected date formats
  71. Alternatively, the `dynamic_date_formats` can be customised to support your
  72. own <<mapping-date-format,date formats>>:
  73. [source,js]
  74. --------------------------------------------------
  75. PUT my_index
  76. {
  77. "mappings": {
  78. "my_type": {
  79. "dynamic_date_formats": ["MM/dd/yyyy"]
  80. }
  81. }
  82. }
  83. PUT my_index/my_type/1
  84. {
  85. "create_date": "09/25/2015"
  86. }
  87. --------------------------------------------------
  88. // AUTOSENSE
  89. [[numeric-detection]]
  90. ==== Numeric detection
  91. While JSON has support for native floating point and integer datatypes, some
  92. applications or languages may sometimes render numbers as strings. Usually the
  93. correct solution is to map these fields explicitly, but numeric detection
  94. (which is disabled by default) can be enabled to do this automatically:
  95. [source,js]
  96. --------------------------------------------------
  97. PUT my_index
  98. {
  99. "mappings": {
  100. "my_type": {
  101. "numeric_detection": true
  102. }
  103. }
  104. }
  105. PUT my_index/my_type/1
  106. {
  107. "my_float": "1.0", <1>
  108. "my_integer": "1" <2>
  109. }
  110. --------------------------------------------------
  111. // AUTOSENSE
  112. <1> The `my_float` field is added as a <<number,`double`>> field.
  113. <2> The `my_integer` field is added as a <<number,`long`>> field.