histogram-aggregation.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. [[search-aggregations-bucket-histogram-aggregation]]
  2. === Histogram Aggregation
  3. A multi-bucket values source based aggregation that can be applied on numeric values extracted from the documents.
  4. It dynamically builds fixed size (a.k.a. interval) buckets over the values. For example, if the documents have a field
  5. that holds a price (numeric), we can configure this aggregation to dynamically build buckets with interval `5`
  6. (in case of price it may represent $5). When the aggregation executes, the price field of every document will be
  7. evaluated and will be rounded down to its closest bucket - for example, if the price is `32` and the bucket size is `5`
  8. then the rounding will yield `30` and thus the document will "fall" into the bucket that is associated with the key `30`.
  9. To make this more formal, here is the rounding function that is used:
  10. [source,java]
  11. --------------------------------------------------
  12. bucket_key = Math.floor((value - offset) / interval) * interval + offset
  13. --------------------------------------------------
  14. The `interval` must be a positive decimal, while the `offset` must be a decimal in `[0, interval)`
  15. (a decimal greater than or equal to `0` and less than `interval`)
  16. The following snippet "buckets" the products based on their `price` by interval of `50`:
  17. [source,console]
  18. --------------------------------------------------
  19. POST /sales/_search?size=0
  20. {
  21. "aggs" : {
  22. "prices" : {
  23. "histogram" : {
  24. "field" : "price",
  25. "interval" : 50
  26. }
  27. }
  28. }
  29. }
  30. --------------------------------------------------
  31. // TEST[setup:sales]
  32. And the following may be the response:
  33. [source,js]
  34. --------------------------------------------------
  35. {
  36. ...
  37. "aggregations": {
  38. "prices" : {
  39. "buckets": [
  40. {
  41. "key": 0.0,
  42. "doc_count": 1
  43. },
  44. {
  45. "key": 50.0,
  46. "doc_count": 1
  47. },
  48. {
  49. "key": 100.0,
  50. "doc_count": 0
  51. },
  52. {
  53. "key": 150.0,
  54. "doc_count": 2
  55. },
  56. {
  57. "key": 200.0,
  58. "doc_count": 3
  59. }
  60. ]
  61. }
  62. }
  63. }
  64. --------------------------------------------------
  65. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  66. ==== Minimum document count
  67. The response above show that no documents has a price that falls within the range of `[100, 150)`. By default the
  68. response will fill gaps in the histogram with empty buckets. It is possible change that and request buckets with
  69. a higher minimum count thanks to the `min_doc_count` setting:
  70. [source,console]
  71. --------------------------------------------------
  72. POST /sales/_search?size=0
  73. {
  74. "aggs" : {
  75. "prices" : {
  76. "histogram" : {
  77. "field" : "price",
  78. "interval" : 50,
  79. "min_doc_count" : 1
  80. }
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. // TEST[setup:sales]
  86. Response:
  87. [source,js]
  88. --------------------------------------------------
  89. {
  90. ...
  91. "aggregations": {
  92. "prices" : {
  93. "buckets": [
  94. {
  95. "key": 0.0,
  96. "doc_count": 1
  97. },
  98. {
  99. "key": 50.0,
  100. "doc_count": 1
  101. },
  102. {
  103. "key": 150.0,
  104. "doc_count": 2
  105. },
  106. {
  107. "key": 200.0,
  108. "doc_count": 3
  109. }
  110. ]
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  116. [[search-aggregations-bucket-histogram-aggregation-extended-bounds]]
  117. By default the `histogram` returns all the buckets within the range of the data itself, that is, the documents with
  118. the smallest values (on which with histogram) will determine the min bucket (the bucket with the smallest key) and the
  119. documents with the highest values will determine the max bucket (the bucket with the highest key). Often, when
  120. requesting empty buckets, this causes a confusion, specifically, when the data is also filtered.
  121. To understand why, let's look at an example:
  122. Lets say the you're filtering your request to get all docs with values between `0` and `500`, in addition you'd like
  123. to slice the data per price using a histogram with an interval of `50`. You also specify `"min_doc_count" : 0` as you'd
  124. like to get all buckets even the empty ones. If it happens that all products (documents) have prices higher than `100`,
  125. 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
  126. to get those buckets between `0 - 100`.
  127. With `extended_bounds` setting, you now can "force" the histogram aggregation to start building buckets on a specific
  128. `min` value and also keep on building buckets up to a `max` value (even if there are no documents anymore). Using
  129. `extended_bounds` only makes sense when `min_doc_count` is 0 (the empty buckets will never be returned if `min_doc_count`
  130. is greater than 0).
  131. Note that (as the name suggest) `extended_bounds` is **not** filtering buckets. Meaning, if the `extended_bounds.min` is higher
  132. than the values extracted from the documents, the documents will still dictate what the first bucket will be (and the
  133. same goes for the `extended_bounds.max` and the last bucket). For filtering buckets, one should nest the histogram aggregation
  134. under a range `filter` aggregation with the appropriate `from`/`to` settings.
  135. Example:
  136. [source,console]
  137. --------------------------------------------------
  138. POST /sales/_search?size=0
  139. {
  140. "query" : {
  141. "constant_score" : { "filter": { "range" : { "price" : { "to" : "500" } } } }
  142. },
  143. "aggs" : {
  144. "prices" : {
  145. "histogram" : {
  146. "field" : "price",
  147. "interval" : 50,
  148. "extended_bounds" : {
  149. "min" : 0,
  150. "max" : 500
  151. }
  152. }
  153. }
  154. }
  155. }
  156. --------------------------------------------------
  157. // TEST[setup:sales]
  158. ==== Order
  159. By default the returned buckets are sorted by their `key` ascending, though the order behaviour can be controlled using
  160. the `order` setting. Supports the same `order` functionality as the <<search-aggregations-bucket-terms-aggregation-order,`Terms Aggregation`>>.
  161. ==== Offset
  162. By default the bucket keys start with 0 and then continue in even spaced steps
  163. of `interval`, e.g. if the interval is `10`, the first three buckets (assuming
  164. there is data inside them) will be `[0, 10)`, `[10, 20)`, `[20, 30)`. The bucket
  165. boundaries can be shifted by using the `offset` option.
  166. 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
  167. 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
  168. documents.
  169. ==== Response Format
  170. By default, the buckets are returned as an ordered array. It is also possible to request the response as a hash
  171. instead keyed by the buckets keys:
  172. [source,console]
  173. --------------------------------------------------
  174. POST /sales/_search?size=0
  175. {
  176. "aggs" : {
  177. "prices" : {
  178. "histogram" : {
  179. "field" : "price",
  180. "interval" : 50,
  181. "keyed" : true
  182. }
  183. }
  184. }
  185. }
  186. --------------------------------------------------
  187. // TEST[setup:sales]
  188. Response:
  189. [source,js]
  190. --------------------------------------------------
  191. {
  192. ...
  193. "aggregations": {
  194. "prices": {
  195. "buckets": {
  196. "0.0": {
  197. "key": 0.0,
  198. "doc_count": 1
  199. },
  200. "50.0": {
  201. "key": 50.0,
  202. "doc_count": 1
  203. },
  204. "100.0": {
  205. "key": 100.0,
  206. "doc_count": 0
  207. },
  208. "150.0": {
  209. "key": 150.0,
  210. "doc_count": 2
  211. },
  212. "200.0": {
  213. "key": 200.0,
  214. "doc_count": 3
  215. }
  216. }
  217. }
  218. }
  219. }
  220. --------------------------------------------------
  221. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  222. ==== Missing value
  223. The `missing` parameter defines how documents that are missing a value should be treated.
  224. By default they will be ignored but it is also possible to treat them as if they
  225. had a value.
  226. [source,console]
  227. --------------------------------------------------
  228. POST /sales/_search?size=0
  229. {
  230. "aggs" : {
  231. "quantity" : {
  232. "histogram" : {
  233. "field" : "quantity",
  234. "interval": 10,
  235. "missing": 0 <1>
  236. }
  237. }
  238. }
  239. }
  240. --------------------------------------------------
  241. // TEST[setup:sales]
  242. <1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`.