1
0

field-mapping.asciidoc 4.1 KB

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