field-mapping.asciidoc 4.1 KB

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