field-mapping.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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,`float`>> 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 a <<text,`text`>> field, with a <<keyword,`keyword`>> sub-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. [[mapping-limit-settings]]
  27. ==== Settings to prevent mappings explosion
  28. Two settings allow to control mapping explosion, in order to prevent adversary
  29. documents to create huge mappings through dynamic mappings for instance:
  30. `index.mapping.total_fields.limit`::
  31. The maximum number of fields in an index. The default value is `1000`.
  32. `index.mapping.depth.limit`::
  33. The maximum depth for a field, which is measured as the number of nested
  34. objects. For instance, if all fields are defined at the root object level,
  35. then the depth is `1`. If there is one object mapping, then the depth is
  36. `2`, etc. The default is `20`.
  37. [[date-detection]]
  38. ==== Date detection
  39. If `date_detection` is enabled (default), then new string fields are checked
  40. to see whether their contents match any of the date patterns specified in
  41. `dynamic_date_formats`. If a match is found, a new <<date,`date`>> field is
  42. added with the corresponding format.
  43. The default value for `dynamic_date_formats` is:
  44. &#91; <<strict-date-time,`"strict_date_optional_time"`>>,`"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`]
  45. For example:
  46. [source,js]
  47. --------------------------------------------------
  48. PUT my_index/my_type/1
  49. {
  50. "create_date": "2015/09/02"
  51. }
  52. GET my_index/_mapping <1>
  53. --------------------------------------------------
  54. // AUTOSENSE
  55. <1> The `create_date` field has been added as a <<date,`date`>>
  56. field with the <<mapping-date-format,`format`>>: +
  57. `"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"`.
  58. ===== Disabling date detection
  59. Dynamic date detection can be disabled by setting `date_detection` to `false`:
  60. [source,js]
  61. --------------------------------------------------
  62. PUT my_index
  63. {
  64. "mappings": {
  65. "my_type": {
  66. "date_detection": false
  67. }
  68. }
  69. }
  70. PUT my_index/my_type/1 <1>
  71. {
  72. "create": "2015/09/02"
  73. }
  74. --------------------------------------------------
  75. // AUTOSENSE
  76. <1> The `create_date` field has been added as a <<text,`text`>> field.
  77. ===== Customising detected date formats
  78. Alternatively, the `dynamic_date_formats` can be customised to support your
  79. own <<mapping-date-format,date formats>>:
  80. [source,js]
  81. --------------------------------------------------
  82. PUT my_index
  83. {
  84. "mappings": {
  85. "my_type": {
  86. "dynamic_date_formats": ["MM/dd/yyyy"]
  87. }
  88. }
  89. }
  90. PUT my_index/my_type/1
  91. {
  92. "create_date": "09/25/2015"
  93. }
  94. --------------------------------------------------
  95. // AUTOSENSE
  96. [[numeric-detection]]
  97. ==== Numeric detection
  98. While JSON has support for native floating point and integer datatypes, some
  99. applications or languages may sometimes render numbers as strings. Usually the
  100. correct solution is to map these fields explicitly, but numeric detection
  101. (which is disabled by default) can be enabled to do this automatically:
  102. [source,js]
  103. --------------------------------------------------
  104. PUT my_index
  105. {
  106. "mappings": {
  107. "my_type": {
  108. "numeric_detection": true
  109. }
  110. }
  111. }
  112. PUT my_index/my_type/1
  113. {
  114. "my_float": "1.0", <1>
  115. "my_integer": "1" <2>
  116. }
  117. --------------------------------------------------
  118. // AUTOSENSE
  119. <1> The `my_float` field is added as a <<number,`double`>> field.
  120. <2> The `my_integer` field is added as a <<number,`long`>> field.