daterange-aggregation.asciidoc 14 KB

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