format.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. [[mapping-date-format]]
  2. === `format`
  3. In JSON documents, dates are represented as strings. Elasticsearch uses a set
  4. of preconfigured formats to recognize and parse these strings into a long
  5. value representing _milliseconds-since-the-epoch_ in UTC.
  6. Besides the <<built-in-date-formats,built-in formats>>, your own
  7. <<custom-date-formats,custom formats>> can be specified using the familiar
  8. `yyyy/MM/dd` syntax:
  9. [source,js]
  10. --------------------------------------------------
  11. PUT my_index
  12. {
  13. "mappings": {
  14. "_doc": {
  15. "properties": {
  16. "date": {
  17. "type": "date",
  18. "format": "yyyy-MM-dd"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. --------------------------------------------------
  25. // CONSOLE
  26. Many APIs which support date values also support <<date-math,date math>>
  27. expressions, such as `now-1m/d` -- the current time, minus one month, rounded
  28. down to the nearest day.
  29. TIP: The `format` setting can be updated on existing fields using the
  30. <<indices-put-mapping,PUT mapping API>>.
  31. [[custom-date-formats]]
  32. ==== Custom date formats
  33. Completely customizable date formats are supported. The syntax for these is explained
  34. http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html[in the Joda docs].
  35. [[built-in-date-formats]]
  36. ==== Built In Formats
  37. Most of the below formats have a `strict` companion format, which means that
  38. year, month and day parts of the week must use respectively 4, 2 and 2 digits
  39. exactly, potentially prepending zeros. For instance a date like `5/11/1` would
  40. be considered invalid and would need to be rewritten to `2005/11/01` to be
  41. accepted by the date parser.
  42. To use them, you need to prepend `strict_` to the name of the date format, for
  43. instance `strict_date_optional_time` instead of `date_optional_time`.
  44. These strict date formats are especially useful when
  45. <<date-detection,date fields are dynamically mapped>> in order to make sure to
  46. not accidentally map irrelevant strings as dates.
  47. The following tables lists all the defaults ISO formats supported:
  48. `epoch_millis`::
  49. A formatter for the number of milliseconds since the epoch. Note, that
  50. this timestamp is subject to the limits of a Java `Long.MIN_VALUE` and
  51. `Long.MAX_VALUE`.
  52. `epoch_second`::
  53. A formatter for the number of seconds since the epoch. Note, that this
  54. timestamp is subject to the limits of a Java `Long.MIN_VALUE` and `Long.
  55. MAX_VALUE` divided by 1000 (the number of milliseconds in a second).
  56. [[strict-date-time]]`date_optional_time` or `strict_date_optional_time`::
  57. A generic ISO datetime parser where the date is mandatory and the time is
  58. optional.
  59. http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser--[Full details here].
  60. `basic_date`::
  61. A basic formatter for a full date as four digit year, two digit month of
  62. year, and two digit day of month: `yyyyMMdd`.
  63. `basic_date_time`::
  64. A basic formatter that combines a basic date and time, separated by a 'T':
  65. `yyyyMMdd'T'HHmmss.SSSZ`.
  66. `basic_date_time_no_millis`::
  67. A basic formatter that combines a basic date and time without millis,
  68. separated by a 'T': `yyyyMMdd'T'HHmmssZ`.
  69. `basic_ordinal_date`::
  70. A formatter for a full ordinal date, using a four digit year and three
  71. digit dayOfYear: `yyyyDDD`.
  72. `basic_ordinal_date_time`::
  73. A formatter for a full ordinal date and time, using a four digit year and
  74. three digit dayOfYear: `yyyyDDD'T'HHmmss.SSSZ`.
  75. `basic_ordinal_date_time_no_millis`::
  76. A formatter for a full ordinal date and time without millis, using a four
  77. digit year and three digit dayOfYear: `yyyyDDD'T'HHmmssZ`.
  78. `basic_time`::
  79. A basic formatter for a two digit hour of day, two digit minute of hour,
  80. two digit second of minute, three digit millis, and time zone offset:
  81. `HHmmss.SSSZ`.
  82. `basic_time_no_millis`::
  83. A basic formatter for a two digit hour of day, two digit minute of hour,
  84. two digit second of minute, and time zone offset: `HHmmssZ`.
  85. `basic_t_time`::
  86. A basic formatter for a two digit hour of day, two digit minute of hour,
  87. two digit second of minute, three digit millis, and time zone off set
  88. prefixed by 'T': `'T'HHmmss.SSSZ`.
  89. `basic_t_time_no_millis`::
  90. A basic formatter for a two digit hour of day, two digit minute of hour,
  91. two digit second of minute, and time zone offset prefixed by 'T':
  92. `'T'HHmmssZ`.
  93. `basic_week_date` or `strict_basic_week_date`::
  94. A basic formatter for a full date as four digit weekyear, two digit week
  95. of weekyear, and one digit day of week: `xxxx'W'wwe`.
  96. `basic_week_date_time` or `strict_basic_week_date_time`::
  97. A basic formatter that combines a basic weekyear date and time, separated
  98. by a 'T': `xxxx'W'wwe'T'HHmmss.SSSZ`.
  99. `basic_week_date_time_no_millis` or `strict_basic_week_date_time_no_millis`::
  100. A basic formatter that combines a basic weekyear date and time without
  101. millis, separated by a 'T': `xxxx'W'wwe'T'HHmmssZ`.
  102. `date` or `strict_date`::
  103. A formatter for a full date as four digit year, two digit month of year,
  104. and two digit day of month: `yyyy-MM-dd`.
  105. `date_hour` or `strict_date_hour`::
  106. A formatter that combines a full date and two digit hour of day:
  107. `yyyy-MM-dd'T'HH`.
  108. `date_hour_minute` or `strict_date_hour_minute`::
  109. A formatter that combines a full date, two digit hour of day, and two
  110. digit minute of hour: `yyyy-MM-dd'T'HH:mm`.
  111. `date_hour_minute_second` or `strict_date_hour_minute_second`::
  112. A formatter that combines a full date, two digit hour of day, two digit
  113. minute of hour, and two digit second of minute: `yyyy-MM-dd'T'HH:mm:ss`.
  114. `date_hour_minute_second_fraction` or `strict_date_hour_minute_second_fraction`::
  115. A formatter that combines a full date, two digit hour of day, two digit
  116. minute of hour, two digit second of minute, and three digit fraction of
  117. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  118. `date_hour_minute_second_millis` or `strict_date_hour_minute_second_millis`::
  119. A formatter that combines a full date, two digit hour of day, two digit
  120. minute of hour, two digit second of minute, and three digit fraction of
  121. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  122. `date_time` or `strict_date_time`::
  123. A formatter that combines a full date and time, separated by a 'T':
  124. `yyyy-MM-dd'T'HH:mm:ss.SSSZZ`.
  125. `date_time_no_millis` or `strict_date_time_no_millis`::
  126. A formatter that combines a full date and time without millis, separated
  127. by a 'T': `yyyy-MM-dd'T'HH:mm:ssZZ`.
  128. `hour` or `strict_hour`::
  129. A formatter for a two digit hour of day: `HH`
  130. `hour_minute` or `strict_hour_minute`::
  131. A formatter for a two digit hour of day and two digit minute of hour:
  132. `HH:mm`.
  133. `hour_minute_second` or `strict_hour_minute_second`::
  134. A formatter for a two digit hour of day, two digit minute of hour, and two
  135. digit second of minute: `HH:mm:ss`.
  136. `hour_minute_second_fraction` or `strict_hour_minute_second_fraction`::
  137. A formatter for a two digit hour of day, two digit minute of hour, two
  138. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  139. `hour_minute_second_millis` or `strict_hour_minute_second_millis`::
  140. A formatter for a two digit hour of day, two digit minute of hour, two
  141. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  142. `ordinal_date` or `strict_ordinal_date`::
  143. A formatter for a full ordinal date, using a four digit year and three
  144. digit dayOfYear: `yyyy-DDD`.
  145. `ordinal_date_time` or `strict_ordinal_date_time`::
  146. A formatter for a full ordinal date and time, using a four digit year and
  147. three digit dayOfYear: `yyyy-DDD'T'HH:mm:ss.SSSZZ`.
  148. `ordinal_date_time_no_millis` or `strict_ordinal_date_time_no_millis`::
  149. A formatter for a full ordinal date and time without millis, using a four
  150. digit year and three digit dayOfYear: `yyyy-DDD'T'HH:mm:ssZZ`.
  151. `time` or `strict_time`::
  152. A formatter for a two digit hour of day, two digit minute of hour, two
  153. digit second of minute, three digit fraction of second, and time zone
  154. offset: `HH:mm:ss.SSSZZ`.
  155. `time_no_millis` or `strict_time_no_millis`::
  156. A formatter for a two digit hour of day, two digit minute of hour, two
  157. digit second of minute, and time zone offset: `HH:mm:ssZZ`.
  158. `t_time` or `strict_t_time`::
  159. A formatter for a two digit hour of day, two digit minute of hour, two
  160. digit second of minute, three digit fraction of second, and time zone
  161. offset prefixed by 'T': `'T'HH:mm:ss.SSSZZ`.
  162. `t_time_no_millis` or `strict_t_time_no_millis`::
  163. A formatter for a two digit hour of day, two digit minute of hour, two
  164. digit second of minute, and time zone offset prefixed by 'T': `'T'HH:mm:ssZZ`.
  165. `week_date` or `strict_week_date`::
  166. A formatter for a full date as four digit weekyear, two digit week of
  167. weekyear, and one digit day of week: `xxxx-'W'ww-e`.
  168. `week_date_time` or `strict_week_date_time`::
  169. A formatter that combines a full weekyear date and time, separated by a
  170. 'T': `xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ`.
  171. `week_date_time_no_millis` or `strict_week_date_time_no_millis`::
  172. A formatter that combines a full weekyear date and time without millis,
  173. separated by a 'T': `xxxx-'W'ww-e'T'HH:mm:ssZZ`.
  174. `weekyear` or `strict_weekyear`::
  175. A formatter for a four digit weekyear: `xxxx`.
  176. `weekyear_week` or `strict_weekyear_week`::
  177. A formatter for a four digit weekyear and two digit week of weekyear:
  178. `xxxx-'W'ww`.
  179. `weekyear_week_day` or `strict_weekyear_week_day`::
  180. A formatter for a four digit weekyear, two digit week of weekyear, and one
  181. digit day of week: `xxxx-'W'ww-e`.
  182. `year` or `strict_year`::
  183. A formatter for a four digit year: `yyyy`.
  184. `year_month` or `strict_year_month`::
  185. A formatter for a four digit year and two digit month of year: `yyyy-MM`.
  186. `year_month_day` or `strict_year_month_day`::
  187. A formatter for a four digit year, two digit month of year, and two digit
  188. day of month: `yyyy-MM-dd`.