histogram-aggregation.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. [[search-aggregations-bucket-histogram-aggregation]]
  2. === Histogram aggregation
  3. ++++
  4. <titleabbrev>Histogram</titleabbrev>
  5. ++++
  6. A multi-bucket values source based aggregation that can be applied on numeric values or numeric range values extracted
  7. from the documents. It dynamically builds fixed size (a.k.a. interval) buckets over the values. For example, if the
  8. documents have a field that holds a price (numeric), we can configure this aggregation to dynamically build buckets with
  9. interval `5` (in case of price it may represent $5). When the aggregation executes, the price field of every document
  10. will be evaluated and will be rounded down to its closest bucket - for example, if the price is `32` and the bucket size
  11. is `5` then the rounding will yield `30` and thus the document will "fall" into the bucket that is associated with the
  12. key `30`.
  13. To make this more formal, here is the rounding function that is used:
  14. [source,java]
  15. --------------------------------------------------
  16. bucket_key = Math.floor((value - offset) / interval) * interval + offset
  17. --------------------------------------------------
  18. For range values, a document can fall into multiple buckets. The first bucket is computed from the lower
  19. bound of the range in the same way as a bucket for a single value is computed. The final bucket is computed in the same
  20. way from the upper bound of the range, and the range is counted in all buckets in between and including those two.
  21. The `interval` must be a positive decimal, while the `offset` must be a decimal in `[0, interval)`
  22. (a decimal greater than or equal to `0` and less than `interval`)
  23. The following snippet "buckets" the products based on their `price` by interval of `50`:
  24. [source,console,id=histogram-aggregation-example]
  25. --------------------------------------------------
  26. POST /sales/_search?size=0
  27. {
  28. "aggs": {
  29. "prices": {
  30. "histogram": {
  31. "field": "price",
  32. "interval": 50
  33. }
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. // TEST[setup:sales]
  39. And the following may be the response:
  40. [source,console-result]
  41. --------------------------------------------------
  42. {
  43. ...
  44. "aggregations": {
  45. "prices": {
  46. "buckets": [
  47. {
  48. "key": 0.0,
  49. "doc_count": 1
  50. },
  51. {
  52. "key": 50.0,
  53. "doc_count": 1
  54. },
  55. {
  56. "key": 100.0,
  57. "doc_count": 0
  58. },
  59. {
  60. "key": 150.0,
  61. "doc_count": 2
  62. },
  63. {
  64. "key": 200.0,
  65. "doc_count": 3
  66. }
  67. ]
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  73. ==== Minimum document count
  74. The response above show that no documents has a price that falls within the range of `[100, 150)`. By default the
  75. response will fill gaps in the histogram with empty buckets. It is possible change that and request buckets with
  76. a higher minimum count thanks to the `min_doc_count` setting:
  77. [source,console,id=histogram-aggregation-min-doc-count-example]
  78. --------------------------------------------------
  79. POST /sales/_search?size=0
  80. {
  81. "aggs": {
  82. "prices": {
  83. "histogram": {
  84. "field": "price",
  85. "interval": 50,
  86. "min_doc_count": 1
  87. }
  88. }
  89. }
  90. }
  91. --------------------------------------------------
  92. // TEST[setup:sales]
  93. Response:
  94. [source,console-result]
  95. --------------------------------------------------
  96. {
  97. ...
  98. "aggregations": {
  99. "prices": {
  100. "buckets": [
  101. {
  102. "key": 0.0,
  103. "doc_count": 1
  104. },
  105. {
  106. "key": 50.0,
  107. "doc_count": 1
  108. },
  109. {
  110. "key": 150.0,
  111. "doc_count": 2
  112. },
  113. {
  114. "key": 200.0,
  115. "doc_count": 3
  116. }
  117. ]
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  123. [[search-aggregations-bucket-histogram-aggregation-extended-bounds]]
  124. By default the `histogram` returns all the buckets within the range of the data itself, that is, the documents with
  125. the smallest values (on which with histogram) will determine the min bucket (the bucket with the smallest key) and the
  126. documents with the highest values will determine the max bucket (the bucket with the highest key). Often, when
  127. requesting empty buckets, this causes a confusion, specifically, when the data is also filtered.
  128. To understand why, let's look at an example:
  129. Lets say the you're filtering your request to get all docs with values between `0` and `500`, in addition you'd like
  130. to slice the data per price using a histogram with an interval of `50`. You also specify `"min_doc_count" : 0` as you'd
  131. like to get all buckets even the empty ones. If it happens that all products (documents) have prices higher than `100`,
  132. the first bucket you'll get will be the one with `100` as its key. This is confusing, as many times, you'd also like
  133. to get those buckets between `0 - 100`.
  134. With `extended_bounds` setting, you now can "force" the histogram aggregation to start building buckets on a specific
  135. `min` value and also keep on building buckets up to a `max` value (even if there are no documents anymore). Using
  136. `extended_bounds` only makes sense when `min_doc_count` is 0 (the empty buckets will never be returned if `min_doc_count`
  137. is greater than 0).
  138. Note that (as the name suggest) `extended_bounds` is **not** filtering buckets. Meaning, if the `extended_bounds.min` is higher
  139. than the values extracted from the documents, the documents will still dictate what the first bucket will be (and the
  140. same goes for the `extended_bounds.max` and the last bucket). For filtering buckets, one should nest the histogram aggregation
  141. under a range `filter` aggregation with the appropriate `from`/`to` settings.
  142. Example:
  143. [source,console,id=histogram-aggregation-extended-bounds-example]
  144. --------------------------------------------------
  145. POST /sales/_search?size=0
  146. {
  147. "query": {
  148. "constant_score": { "filter": { "range": { "price": { "to": "500" } } } }
  149. },
  150. "aggs": {
  151. "prices": {
  152. "histogram": {
  153. "field": "price",
  154. "interval": 50,
  155. "extended_bounds": {
  156. "min": 0,
  157. "max": 500
  158. }
  159. }
  160. }
  161. }
  162. }
  163. --------------------------------------------------
  164. // TEST[setup:sales]
  165. When aggregating ranges, buckets are based on the values of the returned documents. This means the response may include
  166. buckets outside of a query's range. For example, if your query looks for values greater than 100, and you have a range
  167. covering 50 to 150, and an interval of 50, that document will land in 3 buckets - 50, 100, and 150. In general, it's
  168. best to think of the query and aggregation steps as independent - the query selects a set of documents, and then the
  169. aggregation buckets those documents without regard to how they were selected.
  170. See <<search-aggregations-bucket-range-field-note,note on bucketing range
  171. fields>> for more information and an example.
  172. [[search-aggregations-bucket-histogram-aggregation-hard-bounds]]
  173. The `hard_bounds` is a counterpart of `extended_bounds` and can limit the range of buckets in the histogram. It is
  174. particularly useful in the case of open <<range, data ranges>> that can result in a very large number of buckets.
  175. Example:
  176. [source,console,id=histogram-aggregation-hard-bounds-example]
  177. --------------------------------------------------
  178. POST /sales/_search?size=0
  179. {
  180. "query": {
  181. "constant_score": { "filter": { "range": { "price": { "to": "500" } } } }
  182. },
  183. "aggs": {
  184. "prices": {
  185. "histogram": {
  186. "field": "price",
  187. "interval": 50,
  188. "hard_bounds": {
  189. "min": 100,
  190. "max": 200
  191. }
  192. }
  193. }
  194. }
  195. }
  196. --------------------------------------------------
  197. // TEST[setup:sales]
  198. In this example even though the range specified in the query is up to 500, the histogram will only have 2 buckets starting at 100 and 150.
  199. All other buckets will be omitted even if documents that should go to this buckets are present in the results.
  200. ==== Order
  201. By default the returned buckets are sorted by their `key` ascending, though the order behaviour can be controlled using
  202. the `order` setting. Supports the same `order` functionality as the <<search-aggregations-bucket-terms-aggregation-order,`Terms Aggregation`>>.
  203. ==== Offset
  204. By default the bucket keys start with 0 and then continue in even spaced steps
  205. of `interval`, e.g. if the interval is `10`, the first three buckets (assuming
  206. there is data inside them) will be `[0, 10)`, `[10, 20)`, `[20, 30)`. The bucket
  207. boundaries can be shifted by using the `offset` option.
  208. This can be best illustrated with an example. If there are 10 documents with values ranging from 5 to 14, using interval `10` will result in
  209. two buckets with 5 documents each. If an additional offset `5` is used, there will be only one single bucket `[5, 15)` containing all the 10
  210. documents.
  211. ==== Response Format
  212. By default, the buckets are returned as an ordered array. It is also possible to request the response as a hash
  213. instead keyed by the buckets keys:
  214. [source,console,id=histogram-aggregation-keyed-example]
  215. --------------------------------------------------
  216. POST /sales/_search?size=0
  217. {
  218. "aggs": {
  219. "prices": {
  220. "histogram": {
  221. "field": "price",
  222. "interval": 50,
  223. "keyed": true
  224. }
  225. }
  226. }
  227. }
  228. --------------------------------------------------
  229. // TEST[setup:sales]
  230. Response:
  231. [source,console-result]
  232. --------------------------------------------------
  233. {
  234. ...
  235. "aggregations": {
  236. "prices": {
  237. "buckets": {
  238. "0.0": {
  239. "key": 0.0,
  240. "doc_count": 1
  241. },
  242. "50.0": {
  243. "key": 50.0,
  244. "doc_count": 1
  245. },
  246. "100.0": {
  247. "key": 100.0,
  248. "doc_count": 0
  249. },
  250. "150.0": {
  251. "key": 150.0,
  252. "doc_count": 2
  253. },
  254. "200.0": {
  255. "key": 200.0,
  256. "doc_count": 3
  257. }
  258. }
  259. }
  260. }
  261. }
  262. --------------------------------------------------
  263. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  264. ==== Missing value
  265. The `missing` parameter defines how documents that are missing a value should be treated.
  266. By default they will be ignored but it is also possible to treat them as if they
  267. had a value.
  268. [source,console,id=histogram-aggregation-missing-value-example]
  269. --------------------------------------------------
  270. POST /sales/_search?size=0
  271. {
  272. "aggs": {
  273. "quantity": {
  274. "histogram": {
  275. "field": "quantity",
  276. "interval": 10,
  277. "missing": 0 <1>
  278. }
  279. }
  280. }
  281. }
  282. --------------------------------------------------
  283. // TEST[setup:sales]
  284. <1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`.
  285. [[search-aggregations-bucket-histogram-aggregation-histogram-fields]]
  286. ==== Histogram fields
  287. Running a histogram aggregation over histogram fields computes the total number of counts for each interval.
  288. For example, executing a histogram aggregation against the following index that stores pre-aggregated histograms
  289. with latency metrics (in milliseconds) for different networks:
  290. [source,console]
  291. --------------------------------------------------
  292. PUT metrics_index/_doc/1
  293. {
  294. "network.name" : "net-1",
  295. "latency_histo" : {
  296. "values" : [1, 3, 8, 12, 15],
  297. "counts" : [3, 7, 23, 12, 6]
  298. }
  299. }
  300. PUT metrics_index/_doc/2
  301. {
  302. "network.name" : "net-2",
  303. "latency_histo" : {
  304. "values" : [1, 6, 8, 12, 14],
  305. "counts" : [8, 17, 8, 7, 6]
  306. }
  307. }
  308. POST /metrics_index/_search?size=0
  309. {
  310. "aggs": {
  311. "latency_buckets": {
  312. "histogram": {
  313. "field": "latency_histo",
  314. "interval": 5
  315. }
  316. }
  317. }
  318. }
  319. --------------------------------------------------
  320. The `histogram` aggregation will sum the counts of each interval computed based on the `values` and
  321. return the following output:
  322. [source,console-result]
  323. --------------------------------------------------
  324. {
  325. ...
  326. "aggregations": {
  327. "prices": {
  328. "buckets": [
  329. {
  330. "key": 0.0,
  331. "doc_count": 18
  332. },
  333. {
  334. "key": 5.0,
  335. "doc_count": 48
  336. },
  337. {
  338. "key": 10.0,
  339. "doc_count": 25
  340. },
  341. {
  342. "key": 15.0,
  343. "doc_count": 6
  344. }
  345. ]
  346. }
  347. }
  348. }
  349. --------------------------------------------------
  350. // TESTRESPONSE[skip:test not setup]
  351. [IMPORTANT]
  352. ========
  353. Histogram aggregation is a bucket aggregation, which partitions documents into buckets rather than calculating metrics over fields like
  354. metrics aggregations do. Each bucket represents a collection of documents which sub-aggregations can run on.
  355. On the other hand, a histogram field is a pre-aggregated field representing multiple values inside a single field:
  356. buckets of numerical data and a count of items/documents for each bucket. This mismatch between the histogram aggregations expected input
  357. (expecting raw documents) and the histogram field (that provides summary information) limits the outcome of the aggregation
  358. to only the doc counts for each bucket.
  359. **Consequently, when executing a histogram aggregation over a histogram field, no sub-aggregations are allowed.**
  360. ========
  361. Also, when running histogram aggregation over histogram field the `missing` parameter is not supported.