daterange-aggregation.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. [[search-aggregations-bucket-daterange-aggregation]]
  2. === Date range aggregation
  3. ++++
  4. <titleabbrev>Date range</titleabbrev>
  5. ++++
  6. A range aggregation that is dedicated for date values. The main difference
  7. between this aggregation and the normal
  8. <<search-aggregations-bucket-range-aggregation,range>>
  9. aggregation is that the `from` and `to` values can be expressed in
  10. <<date-math,Date Math>> expressions, and it is also possible to specify a date
  11. format by which the `from` and `to` response fields will be returned.
  12. Note that this aggregation includes the `from` value and excludes the `to` value
  13. for each range.
  14. Example:
  15. [source,console,id=daterange-aggregation-example]
  16. --------------------------------------------------
  17. POST /sales/_search?size=0
  18. {
  19. "aggs": {
  20. "range": {
  21. "date_range": {
  22. "field": "date",
  23. "format": "MM-yyyy",
  24. "ranges": [
  25. { "to": "now-10M/M" }, <1>
  26. { "from": "now-10M/M" } <2>
  27. ]
  28. }
  29. }
  30. }
  31. }
  32. --------------------------------------------------
  33. // TEST[setup:sales s/now-10M\/M/10-2015/]
  34. <1> < now minus 10 months, rounded down to the start of the month.
  35. <2> >= now minus 10 months, rounded down to the start of the month.
  36. In the example above, we created two range buckets, the first will "bucket" all
  37. documents dated prior to 10 months ago and the second will "bucket" all
  38. documents dated since 10 months ago
  39. Response:
  40. [source,console-result]
  41. --------------------------------------------------
  42. {
  43. ...
  44. "aggregations": {
  45. "range": {
  46. "buckets": [
  47. {
  48. "to": 1.4436576E12,
  49. "to_as_string": "10-2015",
  50. "doc_count": 7,
  51. "key": "*-10-2015"
  52. },
  53. {
  54. "from": 1.4436576E12,
  55. "from_as_string": "10-2015",
  56. "doc_count": 0,
  57. "key": "10-2015-*"
  58. }
  59. ]
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  65. WARNING: If a format or date value is incomplete, the date range aggregation
  66. replaces any missing components with default values. See
  67. <<missing-date-components>>.
  68. ==== Missing Values
  69. The `missing` parameter defines how documents that are missing a value should
  70. be treated. By default they will be ignored but it is also possible to treat
  71. them as if they had a value. This is done by adding a set of fieldname :
  72. value mappings to specify default values per field.
  73. [source,console,id=daterange-aggregation-missing-example]
  74. --------------------------------------------------
  75. POST /sales/_search?size=0
  76. {
  77. "aggs": {
  78. "range": {
  79. "date_range": {
  80. "field": "date",
  81. "missing": "1976/11/30",
  82. "ranges": [
  83. {
  84. "key": "Older",
  85. "to": "2016/02/01"
  86. }, <1>
  87. {
  88. "key": "Newer",
  89. "from": "2016/02/01",
  90. "to" : "now/d"
  91. }
  92. ]
  93. }
  94. }
  95. }
  96. }
  97. --------------------------------------------------
  98. // TEST[setup:sales]
  99. <1> Documents without a value in the `date` field will be added to the "Older"
  100. bucket, as if they had a date value of "1976-11-30".
  101. [[date-format-pattern]]
  102. ==== Date Format/Pattern
  103. NOTE: this information was copied from
  104. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html[DateTimeFormatter]
  105. All ASCII letters are reserved as format pattern letters, which are defined
  106. as follows:
  107. [options="header"]
  108. |=======
  109. |Symbol |Meaning |Presentation |Examples
  110. |G |era |text |AD; Anno Domini; A
  111. |u |year |year |2004; 04
  112. |y |year-of-era |year |2004; 04
  113. |D |day-of-year |number |189
  114. |M/L |month-of-year |number/text |7; 07; Jul; July; J
  115. |d |day-of-month |number |10
  116. |Q/q |quarter-of-year |number/text |3; 03; Q3; 3rd quarter
  117. |Y |week-based-year |year |1996; 96
  118. |w |week-of-week-based-year |number |27
  119. |W |week-of-month |number |4
  120. |E |day-of-week |text |Tue; Tuesday; T
  121. |e/c |localized day-of-week |number/text |2; 02; Tue; Tuesday; T
  122. |F |week-of-month |number |3
  123. |a |am-pm-of-day |text |PM
  124. |h |clock-hour-of-am-pm (1-12) |number |12
  125. |K |hour-of-am-pm (0-11) |number |0
  126. |k |clock-hour-of-am-pm (1-24) |number |0
  127. |H |hour-of-day (0-23) |number |0
  128. |m |minute-of-hour |number |30
  129. |s |second-of-minute |number |55
  130. |S |fraction-of-second |fraction |978
  131. |A |milli-of-day |number |1234
  132. |n |nano-of-second |number |987654321
  133. |N |nano-of-day |number |1234000000
  134. |V |time-zone ID |zone-id |America/Los_Angeles; Z; -08:30
  135. |z |time-zone name |zone-name |Pacific Standard Time; PST
  136. |O |localized zone-offset |offset-O |GMT+8; GMT+08:00; UTC-08:00;
  137. |X |zone-offset 'Z' for zero |offset-X |Z; -08; -0830; -08:30; -083015; -08:30:15;
  138. |x |zone-offset |offset-x |+0000; -08; -0830; -08:30; -083015; -08:30:15;
  139. |Z |zone-offset |offset-Z |+0000; -0800; -08:00;
  140. |p |pad next |pad modifier |1
  141. |' |escape for text |delimiter
  142. |'' |single quote |literal |'
  143. |[ |optional section start
  144. |] |optional section end
  145. |# |reserved for future use
  146. |{ |reserved for future use
  147. |} |reserved for future use
  148. |=======
  149. The count of pattern letters determines the format.
  150. Text:: The text style is determined based on the number of pattern letters
  151. used. Less than 4 pattern letters will use the short form. Exactly 4
  152. pattern letters will use the full form. Exactly 5 pattern letters will use
  153. the narrow form. Pattern letters `L`, `c`, and `q` specify the stand-alone
  154. form of the text styles.
  155. Number:: If the count of letters is one, then the value is output using
  156. the minimum number of digits and without padding. Otherwise, the count of
  157. digits is used as the width of the output field, with the value
  158. zero-padded as necessary. The following pattern letters have constraints
  159. on the count of letters. Only one letter of `c` and `F` can be specified.
  160. Up to two letters of `d`, `H`, `h`, `K`, `k`, `m`, and `s` can be
  161. specified. Up to three letters of `D` can be specified.
  162. Number/Text:: If the count of pattern letters is 3 or greater, use the
  163. Text rules above. Otherwise use the Number rules above.
  164. Fraction:: Outputs the nano-of-second field as a fraction-of-second. The
  165. nano-of-second value has nine digits, thus the count of pattern letters is
  166. from 1 to 9. If it is less than 9, then the nano-of-second value is
  167. truncated, with only the most significant digits being output.
  168. Year:: The count of letters determines the minimum field width below which
  169. padding is used. If the count of letters is two, then a reduced two digit
  170. form is used. For printing, this outputs the rightmost two digits. For
  171. parsing, this will parse using the base value of 2000, resulting in a year
  172. within the range 2000 to 2099 inclusive. If the count of letters is less
  173. than four (but not two), then the sign is only output for negative years
  174. as per `SignStyle.NORMAL`. Otherwise, the sign is output if the pad width is
  175. exceeded, as per `SignStyle.EXCEEDS_PAD`.
  176. ZoneId:: This outputs the time-zone ID, such as `Europe/Paris`. If the
  177. count of letters is two, then the time-zone ID is output. Any other count
  178. of letters throws `IllegalArgumentException`.
  179. Zone names:: This outputs the display name of the time-zone ID. If the
  180. count of letters is one, two or three, then the short name is output. If
  181. the count of letters is four, then the full name is output. Five or more
  182. letters throws `IllegalArgumentException`.
  183. Offset X and x:: This formats the offset based on the number of pattern
  184. letters. One letter outputs just the hour, such as `+01`, unless the
  185. minute is non-zero in which case the minute is also output, such as
  186. `+0130`. Two letters outputs the hour and minute, without a colon, such as
  187. `+0130`. Three letters outputs the hour and minute, with a colon, such as
  188. `+01:30`. Four letters outputs the hour and minute and optional second,
  189. without a colon, such as `+013015`. Five letters outputs the hour and
  190. minute and optional second, with a colon, such as `+01:30:15`. Six or
  191. more letters throws `IllegalArgumentException`. Pattern letter `X` (upper
  192. case) will output `Z` when the offset to be output would be zero,
  193. whereas pattern letter `x` (lower case) will output `+00`, `+0000`, or
  194. `+00:00`.
  195. Offset O:: This formats the localized offset based on the number of
  196. pattern letters. One letter outputs the short form of the localized
  197. offset, which is localized offset text, such as `GMT`, with hour without
  198. leading zero, optional 2-digit minute and second if non-zero, and colon,
  199. for example `GMT+8`. Four letters outputs the full form, which is
  200. localized offset text, such as `GMT, with 2-digit hour and minute
  201. field, optional second field if non-zero, and colon, for example
  202. `GMT+08:00`. Any other count of letters throws
  203. `IllegalArgumentException`.
  204. Offset Z:: This formats the offset based on the number of pattern letters.
  205. One, two or three letters outputs the hour and minute, without a colon,
  206. such as `+0130`. The output will be `+0000` when the offset is zero.
  207. Four letters outputs the full form of localized offset, equivalent to
  208. four letters of Offset-O. The output will be the corresponding localized
  209. offset text if the offset is zero. Five letters outputs the hour,
  210. minute, with optional second if non-zero, with colon. It outputs `Z` if
  211. the offset is zero. Six or more letters throws IllegalArgumentException.
  212. Optional section:: The optional section markers work exactly like calling
  213. `DateTimeFormatterBuilder.optionalStart()` and
  214. `DateTimeFormatterBuilder.optionalEnd()`.
  215. Pad modifier:: Modifies the pattern that immediately follows to be padded
  216. with spaces. The pad width is determined by the number of pattern letters.
  217. This is the same as calling `DateTimeFormatterBuilder.padNext(int)`.
  218. For example, `ppH` outputs the hour-of-day padded on the left with spaces to a width of 2.
  219. Any unrecognized letter is an error. Any non-letter character, other than
  220. `[`, `]`, `{`, `}`, `#` and the single quote will be output directly.
  221. Despite this, it is recommended to use single quotes around all characters
  222. that you want to output directly to ensure that future changes do not
  223. break your application.
  224. [[time-zones]]
  225. ==== Time zone in date range aggregations
  226. Dates can be converted from another time zone to UTC by specifying the
  227. `time_zone` parameter.
  228. Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or
  229. -08:00) or as one of the time zone ids from the TZ database.
  230. The `time_zone` parameter is also applied to rounding in date math expressions.
  231. As an example, to round to the beginning of the day in the CET time zone, you
  232. can do the following:
  233. [source,console,id=daterange-aggregation-timezone-example]
  234. --------------------------------------------------
  235. POST /sales/_search?size=0
  236. {
  237. "aggs": {
  238. "range": {
  239. "date_range": {
  240. "field": "date",
  241. "time_zone": "CET",
  242. "ranges": [
  243. { "to": "2016/02/01" }, <1>
  244. { "from": "2016/02/01", "to" : "now/d" }, <2>
  245. { "from": "now/d" }
  246. ]
  247. }
  248. }
  249. }
  250. }
  251. --------------------------------------------------
  252. // TEST[setup:sales]
  253. <1> This date will be converted to `2016-02-01T00:00:00.000+01:00`.
  254. <2> `now/d` will be rounded to the beginning of the day in the CET time zone.
  255. ==== Keyed Response
  256. Setting the `keyed` flag to `true` will associate a unique string key with each
  257. bucket and return the ranges as a hash rather than an array:
  258. [source,console,id=daterange-aggregation-keyed-example]
  259. --------------------------------------------------
  260. POST /sales/_search?size=0
  261. {
  262. "aggs": {
  263. "range": {
  264. "date_range": {
  265. "field": "date",
  266. "format": "MM-yyy",
  267. "ranges": [
  268. { "to": "now-10M/M" },
  269. { "from": "now-10M/M" }
  270. ],
  271. "keyed": true
  272. }
  273. }
  274. }
  275. }
  276. --------------------------------------------------
  277. // TEST[setup:sales s/now-10M\/M/10-2015/]
  278. Response:
  279. [source,console-result]
  280. --------------------------------------------------
  281. {
  282. ...
  283. "aggregations": {
  284. "range": {
  285. "buckets": {
  286. "*-10-2015": {
  287. "to": 1.4436576E12,
  288. "to_as_string": "10-2015",
  289. "doc_count": 7
  290. },
  291. "10-2015-*": {
  292. "from": 1.4436576E12,
  293. "from_as_string": "10-2015",
  294. "doc_count": 0
  295. }
  296. }
  297. }
  298. }
  299. }
  300. --------------------------------------------------
  301. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  302. It is also possible to customize the key for each range:
  303. [source,console,id=daterange-aggregation-keyed-multiple-keys-example]
  304. --------------------------------------------------
  305. POST /sales/_search?size=0
  306. {
  307. "aggs": {
  308. "range": {
  309. "date_range": {
  310. "field": "date",
  311. "format": "MM-yyy",
  312. "ranges": [
  313. { "from": "01-2015", "to": "03-2015", "key": "quarter_01" },
  314. { "from": "03-2015", "to": "06-2015", "key": "quarter_02" }
  315. ],
  316. "keyed": true
  317. }
  318. }
  319. }
  320. }
  321. --------------------------------------------------
  322. // TEST[setup:sales]
  323. Response:
  324. [source,console-result]
  325. --------------------------------------------------
  326. {
  327. ...
  328. "aggregations": {
  329. "range": {
  330. "buckets": {
  331. "quarter_01": {
  332. "from": 1.4200704E12,
  333. "from_as_string": "01-2015",
  334. "to": 1.425168E12,
  335. "to_as_string": "03-2015",
  336. "doc_count": 5
  337. },
  338. "quarter_02": {
  339. "from": 1.425168E12,
  340. "from_as_string": "03-2015",
  341. "to": 1.4331168E12,
  342. "to_as_string": "06-2015",
  343. "doc_count": 2
  344. }
  345. }
  346. }
  347. }
  348. }
  349. --------------------------------------------------
  350. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]