histogram-aggregation.asciidoc 9.3 KB

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