autodatehistogram-aggregation.asciidoc 8.0 KB

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