histogram-aggregation.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. The following snippet "buckets" the products based on their `price` by interval of `50`:
  16. [source,js]
  17. --------------------------------------------------
  18. POST /sales/_search?size=0
  19. {
  20. "aggs" : {
  21. "prices" : {
  22. "histogram" : {
  23. "field" : "price",
  24. "interval" : 50
  25. }
  26. }
  27. }
  28. }
  29. --------------------------------------------------
  30. // CONSOLE
  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,js]
  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. // CONSOLE
  86. // TEST[setup:sales]
  87. Response:
  88. [source,js]
  89. --------------------------------------------------
  90. {
  91. ...
  92. "aggregations": {
  93. "prices" : {
  94. "buckets": [
  95. {
  96. "key": 0.0,
  97. "doc_count": 1
  98. },
  99. {
  100. "key": 50.0,
  101. "doc_count": 1
  102. },
  103. {
  104. "key": 150.0,
  105. "doc_count": 2
  106. },
  107. {
  108. "key": 200.0,
  109. "doc_count": 3
  110. }
  111. ]
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  117. [[search-aggregations-bucket-histogram-aggregation-extended-bounds]]
  118. By default the `histogram` returns all the buckets within the range of the data itself, that is, the documents with
  119. the smallest values (on which with histogram) will determine the min bucket (the bucket with the smallest key) and the
  120. documents with the highest values will determine the max bucket (the bucket with the highest key). Often, when
  121. requesting empty buckets, this causes a confusion, specifically, when the data is also filtered.
  122. To understand why, let's look at an example:
  123. Lets say the you're filtering your request to get all docs with values between `0` and `500`, in addition you'd like
  124. to slice the data per price using a histogram with an interval of `50`. You also specify `"min_doc_count" : 0` as you'd
  125. like to get all buckets even the empty ones. If it happens that all products (documents) have prices higher than `100`,
  126. 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
  127. to get those buckets between `0 - 100`.
  128. With `extended_bounds` setting, you now can "force" the histogram aggregation to start building buckets on a specific
  129. `min` values and also keep on building buckets up to a `max` value (even if there are no documents anymore). Using
  130. `extended_bounds` only makes sense when `min_doc_count` is 0 (the empty buckets will never be returned if `min_doc_count`
  131. is greater than 0).
  132. Note that (as the name suggest) `extended_bounds` is **not** filtering buckets. Meaning, if the `extended_bounds.min` is higher
  133. than the values extracted from the documents, the documents will still dictate what the first bucket will be (and the
  134. same goes for the `extended_bounds.max` and the last bucket). For filtering buckets, one should nest the histogram aggregation
  135. under a range `filter` aggregation with the appropriate `from`/`to` settings.
  136. Example:
  137. [source,js]
  138. --------------------------------------------------
  139. POST /sales/_search?size=0
  140. {
  141. "query" : {
  142. "constant_score" : { "filter": { "range" : { "price" : { "to" : "500" } } } }
  143. },
  144. "aggs" : {
  145. "prices" : {
  146. "histogram" : {
  147. "field" : "price",
  148. "interval" : 50,
  149. "extended_bounds" : {
  150. "min" : 0,
  151. "max" : 500
  152. }
  153. }
  154. }
  155. }
  156. }
  157. --------------------------------------------------
  158. // CONSOLE
  159. // TEST[setup:sales]
  160. ==== Order
  161. By default the returned buckets are sorted by their `key` ascending, though the order behaviour can be controlled
  162. using the `order` setting.
  163. Ordering the buckets by their key - descending:
  164. [source,js]
  165. --------------------------------------------------
  166. POST /sales/_search?size=0
  167. {
  168. "aggs" : {
  169. "prices" : {
  170. "histogram" : {
  171. "field" : "price",
  172. "interval" : 50,
  173. "order" : { "_key" : "desc" }
  174. }
  175. }
  176. }
  177. }
  178. --------------------------------------------------
  179. // CONSOLE
  180. // TEST[setup:sales]
  181. Ordering the buckets by their `doc_count` - ascending:
  182. [source,js]
  183. --------------------------------------------------
  184. POST /sales/_search?size=0
  185. {
  186. "aggs" : {
  187. "prices" : {
  188. "histogram" : {
  189. "field" : "price",
  190. "interval" : 50,
  191. "order" : { "_count" : "asc" }
  192. }
  193. }
  194. }
  195. }
  196. --------------------------------------------------
  197. // CONSOLE
  198. // TEST[setup:sales]
  199. If the histogram aggregation has a direct metrics sub-aggregation, the latter can determine the order of the buckets:
  200. [source,js]
  201. --------------------------------------------------
  202. POST /sales/_search?size=0
  203. {
  204. "aggs" : {
  205. "prices" : {
  206. "histogram" : {
  207. "field" : "price",
  208. "interval" : 50,
  209. "order" : { "price_stats.min" : "asc" } <1>
  210. },
  211. "aggs" : {
  212. "price_stats" : { "stats" : {"field" : "price"} }
  213. }
  214. }
  215. }
  216. }
  217. --------------------------------------------------
  218. // CONSOLE
  219. // TEST[setup:sales]
  220. <1> The `{ "price_stats.min" : asc" }` will sort the buckets based on `min` value of their `price_stats` sub-aggregation.
  221. It is also possible to order the buckets based on a "deeper" aggregation in the hierarchy. This is supported as long
  222. as the aggregations path are of a single-bucket type, where the last aggregation in the path may either by a single-bucket
  223. one or a metrics one. If it's a single-bucket type, the order will be defined by the number of docs in the bucket (i.e. `doc_count`),
  224. in case it's a metrics one, the same rules as above apply (where the path must indicate the metric name to sort by in case of
  225. a multi-value metrics aggregation, and in case of a single-value metrics aggregation the sort will be applied on that value).
  226. The path must be defined in the following form:
  227. // https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form
  228. [source,ebnf]
  229. --------------------------------------------------
  230. AGG_SEPARATOR = '>' ;
  231. METRIC_SEPARATOR = '.' ;
  232. AGG_NAME = <the name of the aggregation> ;
  233. METRIC = <the name of the metric (in case of multi-value metrics aggregation)> ;
  234. PATH = <AGG_NAME> [ <AGG_SEPARATOR>, <AGG_NAME> ]* [ <METRIC_SEPARATOR>, <METRIC> ] ;
  235. --------------------------------------------------
  236. [source,js]
  237. --------------------------------------------------
  238. POST /sales/_search?size=0
  239. {
  240. "aggs" : {
  241. "prices" : {
  242. "histogram" : {
  243. "field" : "price",
  244. "interval" : 50,
  245. "order" : { "promoted_products>rating_stats.avg" : "desc" } <1>
  246. },
  247. "aggs" : {
  248. "promoted_products" : {
  249. "filter" : { "term" : { "promoted" : true }},
  250. "aggs" : {
  251. "rating_stats" : { "stats" : { "field" : "rating" }}
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. --------------------------------------------------
  259. // CONSOLE
  260. // TEST[setup:sales]
  261. The above will sort the buckets based on the avg rating among the promoted products
  262. ==== Offset
  263. 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
  264. (assuming there is data inside them) will be [0 - 9], [10-19], [20-29]. The bucket boundaries can be shifted by using the `offset` option.
  265. 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
  266. two buckets with 5 documents each. If an additional offset `5` is used, there will be only one single bucket [5-14] containing all the 10
  267. documents.
  268. ==== Response Format
  269. By default, the buckets are returned as an ordered array. It is also possible to request the response as a hash
  270. instead keyed by the buckets keys:
  271. [source,js]
  272. --------------------------------------------------
  273. POST /sales/_search?size=0
  274. {
  275. "aggs" : {
  276. "prices" : {
  277. "histogram" : {
  278. "field" : "price",
  279. "interval" : 50,
  280. "keyed" : true
  281. }
  282. }
  283. }
  284. }
  285. --------------------------------------------------
  286. // CONSOLE
  287. // TEST[setup:sales]
  288. Response:
  289. [source,js]
  290. --------------------------------------------------
  291. {
  292. ...
  293. "aggregations": {
  294. "prices": {
  295. "buckets": {
  296. "0.0": {
  297. "key": 0.0,
  298. "doc_count": 1
  299. },
  300. "50.0": {
  301. "key": 50.0,
  302. "doc_count": 1
  303. },
  304. "100.0": {
  305. "key": 100.0,
  306. "doc_count": 0
  307. },
  308. "150.0": {
  309. "key": 150.0,
  310. "doc_count": 2
  311. },
  312. "200.0": {
  313. "key": 200.0,
  314. "doc_count": 3
  315. }
  316. }
  317. }
  318. }
  319. }
  320. --------------------------------------------------
  321. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  322. ==== Missing value
  323. The `missing` parameter defines how documents that are missing a value should be treated.
  324. By default they will be ignored but it is also possible to treat them as if they
  325. had a value.
  326. [source,js]
  327. --------------------------------------------------
  328. POST /sales/_search?size=0
  329. {
  330. "aggs" : {
  331. "quantity" : {
  332. "histogram" : {
  333. "field" : "quantity",
  334. "interval": 10,
  335. "missing": 0 <1>
  336. }
  337. }
  338. }
  339. }
  340. --------------------------------------------------
  341. // CONSOLE
  342. // TEST[setup:sales]
  343. <1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`.