range-aggregation.asciidoc 11 KB

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