autodatehistogram-aggregation.asciidoc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. [[search-aggregations-bucket-autodatehistogram-aggregation]]
  2. === Auto-interval date histogram aggregation
  3. ++++
  4. <titleabbrev>Auto-interval date histogram</titleabbrev>
  5. ++++
  6. A multi-bucket aggregation similar to the <<search-aggregations-bucket-datehistogram-aggregation>> except
  7. instead of providing an interval to use as the width of each bucket, a target number of buckets is provided
  8. indicating the number of buckets needed and the interval of the buckets is automatically chosen to best achieve
  9. that target. The number of buckets returned will always be less than or equal to this target number.
  10. The buckets field is optional, and will default to 10 buckets if not specified.
  11. Requesting a target of 10 buckets.
  12. [source,console,id=autodatehistogram-aggregation-example]
  13. --------------------------------------------------
  14. POST /sales/_search?size=0
  15. {
  16. "aggs": {
  17. "sales_over_time": {
  18. "auto_date_histogram": {
  19. "field": "date",
  20. "buckets": 10
  21. }
  22. }
  23. }
  24. }
  25. --------------------------------------------------
  26. // TEST[setup:sales]
  27. ==== Keys
  28. Internally, a date is represented as a 64 bit number representing a timestamp
  29. in milliseconds-since-the-epoch. These timestamps are returned as the bucket
  30. ++key++s. The `key_as_string` is the same timestamp converted to a formatted
  31. date string using the format specified with the `format` parameter:
  32. TIP: If no `format` is specified, then it will use the first date
  33. <<mapping-date-format,format>> specified in the field mapping.
  34. [source,console,id=autodatehistogram-aggregation-format-example]
  35. --------------------------------------------------
  36. POST /sales/_search?size=0
  37. {
  38. "aggs": {
  39. "sales_over_time": {
  40. "auto_date_histogram": {
  41. "field": "date",
  42. "buckets": 5,
  43. "format": "yyyy-MM-dd" <1>
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. // TEST[setup:sales]
  50. <1> Supports expressive date <<date-format-pattern,format pattern>>
  51. Response:
  52. [source,console-result]
  53. --------------------------------------------------
  54. {
  55. ...
  56. "aggregations": {
  57. "sales_over_time": {
  58. "buckets": [
  59. {
  60. "key_as_string": "2015-01-01",
  61. "key": 1420070400000,
  62. "doc_count": 3
  63. },
  64. {
  65. "key_as_string": "2015-02-01",
  66. "key": 1422748800000,
  67. "doc_count": 2
  68. },
  69. {
  70. "key_as_string": "2015-03-01",
  71. "key": 1425168000000,
  72. "doc_count": 2
  73. }
  74. ],
  75. "interval": "1M"
  76. }
  77. }
  78. }
  79. --------------------------------------------------
  80. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  81. ==== Intervals
  82. The interval of the returned buckets is selected based on the data collected by the
  83. aggregation so that the number of buckets returned is less than or equal to the number
  84. requested. The possible intervals returned are:
  85. [horizontal]
  86. seconds:: In multiples of 1, 5, 10 and 30
  87. minutes:: In multiples of 1, 5, 10 and 30
  88. hours:: In multiples of 1, 3 and 12
  89. days:: In multiples of 1, and 7
  90. months:: In multiples of 1, and 3
  91. years:: In multiples of 1, 5, 10, 20, 50 and 100
  92. In the worst case, where the number of daily buckets are too many for the requested
  93. number of buckets, the number of buckets returned will be 1/7th of the number of
  94. buckets requested.
  95. ==== Time Zone
  96. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  97. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  98. that bucketing should use a different time zone.
  99. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  100. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  101. `America/Los_Angeles`.
  102. Consider the following example:
  103. [source,console,id=autodatehistogram-aggregation-timezone-example]
  104. ---------------------------------
  105. PUT my-index-000001/_doc/1?refresh
  106. {
  107. "date": "2015-10-01T00:30:00Z"
  108. }
  109. PUT my-index-000001/_doc/2?refresh
  110. {
  111. "date": "2015-10-01T01:30:00Z"
  112. }
  113. PUT my-index-000001/_doc/3?refresh
  114. {
  115. "date": "2015-10-01T02:30:00Z"
  116. }
  117. GET my-index-000001/_search?size=0
  118. {
  119. "aggs": {
  120. "by_day": {
  121. "auto_date_histogram": {
  122. "field": "date",
  123. "buckets" : 3
  124. }
  125. }
  126. }
  127. }
  128. ---------------------------------
  129. UTC is used if no time zone is specified, three 1-hour buckets are returned
  130. starting at midnight UTC on 1 October 2015:
  131. [source,console-result]
  132. ---------------------------------
  133. {
  134. ...
  135. "aggregations": {
  136. "by_day": {
  137. "buckets": [
  138. {
  139. "key_as_string": "2015-10-01T00:00:00.000Z",
  140. "key": 1443657600000,
  141. "doc_count": 1
  142. },
  143. {
  144. "key_as_string": "2015-10-01T01:00:00.000Z",
  145. "key": 1443661200000,
  146. "doc_count": 1
  147. },
  148. {
  149. "key_as_string": "2015-10-01T02:00:00.000Z",
  150. "key": 1443664800000,
  151. "doc_count": 1
  152. }
  153. ],
  154. "interval": "1h"
  155. }
  156. }
  157. }
  158. ---------------------------------
  159. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  160. If a `time_zone` of `-01:00` is specified, then midnight starts at one hour before
  161. midnight UTC:
  162. [source,console]
  163. ---------------------------------
  164. GET my-index-000001/_search?size=0
  165. {
  166. "aggs": {
  167. "by_day": {
  168. "auto_date_histogram": {
  169. "field": "date",
  170. "buckets" : 3,
  171. "time_zone": "-01:00"
  172. }
  173. }
  174. }
  175. }
  176. ---------------------------------
  177. // TEST[continued]
  178. Now three 1-hour buckets are still returned but the first bucket starts at
  179. 11:00pm on 30 September 2015 since that is the local time for the bucket in
  180. the specified time zone.
  181. [source,console-result]
  182. ---------------------------------
  183. {
  184. ...
  185. "aggregations": {
  186. "by_day": {
  187. "buckets": [
  188. {
  189. "key_as_string": "2015-09-30T23:00:00.000-01:00", <1>
  190. "key": 1443657600000,
  191. "doc_count": 1
  192. },
  193. {
  194. "key_as_string": "2015-10-01T00:00:00.000-01:00",
  195. "key": 1443661200000,
  196. "doc_count": 1
  197. },
  198. {
  199. "key_as_string": "2015-10-01T01:00:00.000-01:00",
  200. "key": 1443664800000,
  201. "doc_count": 1
  202. }
  203. ],
  204. "interval": "1h"
  205. }
  206. }
  207. }
  208. ---------------------------------
  209. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  210. <1> The `key_as_string` value represents midnight on each day
  211. in the specified time zone.
  212. WARNING: When using time zones that follow DST (daylight savings time) changes,
  213. buckets close to the moment when those changes happen can have slightly different
  214. sizes than neighbouring buckets.
  215. For example, consider a DST start in the `CET` time zone: on 27 March 2016 at 2am,
  216. clocks were turned forward 1 hour to 3am local time. If the result of the aggregation
  217. was daily buckets, the bucket covering that day will only hold data for 23 hours
  218. instead of the usual 24 hours for other buckets. The same is true for shorter intervals
  219. like e.g. 12h. Here, we will have only a 11h bucket on the morning of 27 March when the
  220. DST shift happens.
  221. ==== Minimum Interval parameter
  222. The `minimum_interval` allows the caller to specify the minimum rounding interval that should be used.
  223. This can make the collection process more efficient, as the aggregation will not attempt to round at
  224. any interval lower than `minimum_interval`.
  225. The accepted units for `minimum_interval` are:
  226. * year
  227. * month
  228. * day
  229. * hour
  230. * minute
  231. * second
  232. [source,console,id=autodatehistogram-aggregation-minimum-interval-example]
  233. --------------------------------------------------
  234. POST /sales/_search?size=0
  235. {
  236. "aggs": {
  237. "sale_date": {
  238. "auto_date_histogram": {
  239. "field": "date",
  240. "buckets": 10,
  241. "minimum_interval": "minute"
  242. }
  243. }
  244. }
  245. }
  246. --------------------------------------------------
  247. // TEST[setup:sales]
  248. ==== Missing value
  249. The `missing` parameter defines how documents that are missing a value should be treated.
  250. By default they will be ignored but it is also possible to treat them as if they
  251. had a value.
  252. [source,console,id=autodatehistogram-aggregation-missing-example]
  253. --------------------------------------------------
  254. POST /sales/_search?size=0
  255. {
  256. "aggs": {
  257. "sale_date": {
  258. "auto_date_histogram": {
  259. "field": "date",
  260. "buckets": 10,
  261. "missing": "2000/01/01" <1>
  262. }
  263. }
  264. }
  265. }
  266. --------------------------------------------------
  267. // TEST[setup:sales]
  268. <1> Documents without a value in the `publish_date` field will fall into the same bucket as documents that have the value `2000-01-01`.