range-aggregation.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. [[search-aggregations-bucket-range-aggregation]]
  2. === Range aggregation
  3. ++++
  4. <titleabbrev>Range</titleabbrev>
  5. ++++
  6. A multi-bucket value source based aggregation that enables the user to define a set of ranges - each representing a bucket. During the aggregation process, the values extracted from each document will be checked against each bucket range and "bucket" the relevant/matching document.
  7. Note that this aggregation includes the `from` value and excludes the `to` value for each range.
  8. Example:
  9. [source,console,id=range-aggregation-example]
  10. --------------------------------------------------
  11. GET /_search
  12. {
  13. "aggs": {
  14. "price_ranges": {
  15. "range": {
  16. "field": "price",
  17. "ranges": [
  18. { "to": 100.0 },
  19. { "from": 100.0, "to": 200.0 },
  20. { "from": 200.0 }
  21. ]
  22. }
  23. }
  24. }
  25. }
  26. --------------------------------------------------
  27. // TEST[setup:sales]
  28. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  29. Response:
  30. [source,console-result]
  31. --------------------------------------------------
  32. {
  33. ...
  34. "aggregations": {
  35. "price_ranges": {
  36. "buckets": [
  37. {
  38. "key": "*-100.0",
  39. "to": 100.0,
  40. "doc_count": 2
  41. },
  42. {
  43. "key": "100.0-200.0",
  44. "from": 100.0,
  45. "to": 200.0,
  46. "doc_count": 2
  47. },
  48. {
  49. "key": "200.0-*",
  50. "from": 200.0,
  51. "doc_count": 3
  52. }
  53. ]
  54. }
  55. }
  56. }
  57. --------------------------------------------------
  58. // TESTRESPONSE[s/\.\.\.//]
  59. ==== Keyed Response
  60. Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
  61. [source,console,id=range-aggregation-keyed-example]
  62. --------------------------------------------------
  63. GET /_search
  64. {
  65. "aggs": {
  66. "price_ranges": {
  67. "range": {
  68. "field": "price",
  69. "keyed": true,
  70. "ranges": [
  71. { "to": 100 },
  72. { "from": 100, "to": 200 },
  73. { "from": 200 }
  74. ]
  75. }
  76. }
  77. }
  78. }
  79. --------------------------------------------------
  80. // TEST[setup:sales]
  81. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  82. Response:
  83. [source,console-result]
  84. --------------------------------------------------
  85. {
  86. ...
  87. "aggregations": {
  88. "price_ranges": {
  89. "buckets": {
  90. "*-100.0": {
  91. "to": 100.0,
  92. "doc_count": 2
  93. },
  94. "100.0-200.0": {
  95. "from": 100.0,
  96. "to": 200.0,
  97. "doc_count": 2
  98. },
  99. "200.0-*": {
  100. "from": 200.0,
  101. "doc_count": 3
  102. }
  103. }
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // TESTRESPONSE[s/\.\.\.//]
  109. It is also possible to customize the key for each range:
  110. [source,console,id=range-aggregation-custom-keys-example]
  111. --------------------------------------------------
  112. GET /_search
  113. {
  114. "aggs": {
  115. "price_ranges": {
  116. "range": {
  117. "field": "price",
  118. "keyed": true,
  119. "ranges": [
  120. { "key": "cheap", "to": 100 },
  121. { "key": "average", "from": 100, "to": 200 },
  122. { "key": "expensive", "from": 200 }
  123. ]
  124. }
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. // TEST[setup:sales]
  130. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  131. Response:
  132. [source,console-result]
  133. --------------------------------------------------
  134. {
  135. ...
  136. "aggregations": {
  137. "price_ranges": {
  138. "buckets": {
  139. "cheap": {
  140. "to": 100.0,
  141. "doc_count": 2
  142. },
  143. "average": {
  144. "from": 100.0,
  145. "to": 200.0,
  146. "doc_count": 2
  147. },
  148. "expensive": {
  149. "from": 200.0,
  150. "doc_count": 3
  151. }
  152. }
  153. }
  154. }
  155. }
  156. --------------------------------------------------
  157. // TESTRESPONSE[s/\.\.\.//]
  158. ==== Script
  159. Range aggregation accepts a `script` parameter. This parameter allows to defined an inline `script` that
  160. will be executed during aggregation execution.
  161. The following example shows how to use an `inline` script with the `painless` script language and no script parameters:
  162. [source,console,id=range-aggregation-script-example]
  163. --------------------------------------------------
  164. GET /_search
  165. {
  166. "aggs": {
  167. "price_ranges": {
  168. "range": {
  169. "script": {
  170. "lang": "painless",
  171. "source": "doc['price'].value"
  172. },
  173. "ranges": [
  174. { "to": 100 },
  175. { "from": 100, "to": 200 },
  176. { "from": 200 }
  177. ]
  178. }
  179. }
  180. }
  181. }
  182. --------------------------------------------------
  183. It is also possible to use stored scripts. Here is a simple stored script:
  184. [source,console,id=range-aggregation-stored-script-example]
  185. --------------------------------------------------
  186. POST /_scripts/convert_currency
  187. {
  188. "script": {
  189. "lang": "painless",
  190. "source": "doc[params.field].value * params.conversion_rate"
  191. }
  192. }
  193. --------------------------------------------------
  194. // TEST[setup:sales]
  195. And this new stored script can be used in the range aggregation like this:
  196. [source,console]
  197. --------------------------------------------------
  198. GET /_search
  199. {
  200. "aggs": {
  201. "price_ranges": {
  202. "range": {
  203. "script": {
  204. "id": "convert_currency", <1>
  205. "params": { <2>
  206. "field": "price",
  207. "conversion_rate": 0.835526591
  208. }
  209. },
  210. "ranges": [
  211. { "from": 0, "to": 100 },
  212. { "from": 100 }
  213. ]
  214. }
  215. }
  216. }
  217. }
  218. --------------------------------------------------
  219. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  220. // TEST[continued]
  221. <1> Id of the stored script
  222. <2> Parameters to use when executing the stored script
  223. //////////////////////////
  224. [source,console-result]
  225. --------------------------------------------------
  226. {
  227. "aggregations": {
  228. "price_ranges": {
  229. "buckets": [
  230. {
  231. "key": "0.0-100.0",
  232. "from": 0.0,
  233. "to": 100.0,
  234. "doc_count": 2
  235. },
  236. {
  237. "key": "100.0-*",
  238. "from": 100.0,
  239. "doc_count": 5
  240. }
  241. ]
  242. }
  243. }
  244. }
  245. --------------------------------------------------
  246. //////////////////////////
  247. ==== Value Script
  248. Lets say the product prices are in USD but we would like to get the price ranges in EURO. We can use value script to convert the prices prior the aggregation (assuming conversion rate of 0.8)
  249. [source,console,id=range-aggregation-value-script-example]
  250. --------------------------------------------------
  251. GET /sales/_search
  252. {
  253. "aggs": {
  254. "price_ranges": {
  255. "range": {
  256. "field": "price",
  257. "script": {
  258. "source": "_value * params.conversion_rate",
  259. "params": {
  260. "conversion_rate": 0.8
  261. }
  262. },
  263. "ranges": [
  264. { "to": 35 },
  265. { "from": 35, "to": 70 },
  266. { "from": 70 }
  267. ]
  268. }
  269. }
  270. }
  271. }
  272. --------------------------------------------------
  273. // TEST[setup:sales]
  274. ==== Sub Aggregations
  275. The following example, not only "bucket" the documents to the different buckets but also computes statistics over the prices in each price range
  276. [source,console,id=range-aggregation-sub-aggregation-example]
  277. --------------------------------------------------
  278. GET /_search
  279. {
  280. "aggs": {
  281. "price_ranges": {
  282. "range": {
  283. "field": "price",
  284. "ranges": [
  285. { "to": 100 },
  286. { "from": 100, "to": 200 },
  287. { "from": 200 }
  288. ]
  289. },
  290. "aggs": {
  291. "price_stats": {
  292. "stats": { "field": "price" }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. --------------------------------------------------
  299. // TEST[setup:sales]
  300. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  301. Response:
  302. [source,console-result]
  303. --------------------------------------------------
  304. {
  305. ...
  306. "aggregations": {
  307. "price_ranges": {
  308. "buckets": [
  309. {
  310. "key": "*-100.0",
  311. "to": 100.0,
  312. "doc_count": 2,
  313. "price_stats": {
  314. "count": 2,
  315. "min": 10.0,
  316. "max": 50.0,
  317. "avg": 30.0,
  318. "sum": 60.0
  319. }
  320. },
  321. {
  322. "key": "100.0-200.0",
  323. "from": 100.0,
  324. "to": 200.0,
  325. "doc_count": 2,
  326. "price_stats": {
  327. "count": 2,
  328. "min": 150.0,
  329. "max": 175.0,
  330. "avg": 162.5,
  331. "sum": 325.0
  332. }
  333. },
  334. {
  335. "key": "200.0-*",
  336. "from": 200.0,
  337. "doc_count": 3,
  338. "price_stats": {
  339. "count": 3,
  340. "min": 200.0,
  341. "max": 200.0,
  342. "avg": 200.0,
  343. "sum": 600.0
  344. }
  345. }
  346. ]
  347. }
  348. }
  349. }
  350. --------------------------------------------------
  351. // TESTRESPONSE[s/\.\.\.//]