rate-aggregation.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[search-aggregations-metrics-rate-aggregation]]
  4. === Rate Aggregation
  5. A `rate` metrics aggregation can be used only inside a `date_histogram` and calculates a rate of documents or a field in each
  6. `date_histogram` bucket. The field values can be generated by a provided script or extracted from specific numeric or
  7. <<histogram,histogram fields>> in the documents.
  8. ==== Syntax
  9. A `rate` aggregation looks like this in isolation:
  10. [source,js]
  11. --------------------------------------------------
  12. {
  13. "rate": {
  14. "unit": "month",
  15. "field": "requests"
  16. }
  17. }
  18. --------------------------------------------------
  19. // NOTCONSOLE
  20. The following request will group all sales records into monthly bucket and than convert the number of sales transaction in each bucket
  21. into per annual sales rate.
  22. [source,console]
  23. --------------------------------------------------
  24. GET sales/_search
  25. {
  26. "size": 0,
  27. "aggs": {
  28. "by_date": {
  29. "date_histogram": {
  30. "field": "date",
  31. "calendar_interval": "month" <1>
  32. },
  33. "aggs": {
  34. "my_rate": {
  35. "rate": {
  36. "unit": "year" <2>
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. // TEST[setup:sales]
  45. <1> Histogram is grouped by month.
  46. <2> But the rate is converted into annual rate.
  47. The response will return the annual rate of transaction in each bucket. Since there are 12 months per year, the annual rate will
  48. be automatically calculated by multiplying monthly rate by 12.
  49. [source,console-result]
  50. --------------------------------------------------
  51. {
  52. ...
  53. "aggregations" : {
  54. "by_date" : {
  55. "buckets" : [
  56. {
  57. "key_as_string" : "2015/01/01 00:00:00",
  58. "key" : 1420070400000,
  59. "doc_count" : 3,
  60. "my_rate" : {
  61. "value" : 36.0
  62. }
  63. },
  64. {
  65. "key_as_string" : "2015/02/01 00:00:00",
  66. "key" : 1422748800000,
  67. "doc_count" : 2,
  68. "my_rate" : {
  69. "value" : 24.0
  70. }
  71. },
  72. {
  73. "key_as_string" : "2015/03/01 00:00:00",
  74. "key" : 1425168000000,
  75. "doc_count" : 2,
  76. "my_rate" : {
  77. "value" : 24.0
  78. }
  79. }
  80. ]
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  86. Instead of counting the number of documents, it is also possible to calculate a sum of all values of the fields in the documents in each
  87. bucket or the number of values in each bucket. The following request will group all sales records into monthly bucket and than calculate
  88. the total monthly sales and convert them into average daily sales.
  89. [source,console]
  90. --------------------------------------------------
  91. GET sales/_search
  92. {
  93. "size": 0,
  94. "aggs": {
  95. "by_date": {
  96. "date_histogram": {
  97. "field": "date",
  98. "calendar_interval": "month" <1>
  99. },
  100. "aggs": {
  101. "avg_price": {
  102. "rate": {
  103. "field": "price", <2>
  104. "unit": "day" <3>
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. --------------------------------------------------
  112. // TEST[setup:sales]
  113. <1> Histogram is grouped by month.
  114. <2> Calculate sum of all sale prices
  115. <3> Convert to average daily sales
  116. The response will contain the average daily sale prices for each month.
  117. [source,console-result]
  118. --------------------------------------------------
  119. {
  120. ...
  121. "aggregations" : {
  122. "by_date" : {
  123. "buckets" : [
  124. {
  125. "key_as_string" : "2015/01/01 00:00:00",
  126. "key" : 1420070400000,
  127. "doc_count" : 3,
  128. "avg_price" : {
  129. "value" : 17.741935483870968
  130. }
  131. },
  132. {
  133. "key_as_string" : "2015/02/01 00:00:00",
  134. "key" : 1422748800000,
  135. "doc_count" : 2,
  136. "avg_price" : {
  137. "value" : 2.142857142857143
  138. }
  139. },
  140. {
  141. "key_as_string" : "2015/03/01 00:00:00",
  142. "key" : 1425168000000,
  143. "doc_count" : 2,
  144. "avg_price" : {
  145. "value" : 12.096774193548388
  146. }
  147. }
  148. ]
  149. }
  150. }
  151. }
  152. --------------------------------------------------
  153. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  154. By adding the `mode` parameter with the value `value_count`, we can change the calculation from `sum` to the number of values of the field:
  155. [source,console]
  156. --------------------------------------------------
  157. GET sales/_search
  158. {
  159. "size": 0,
  160. "aggs": {
  161. "by_date": {
  162. "date_histogram": {
  163. "field": "date",
  164. "calendar_interval": "month" <1>
  165. },
  166. "aggs": {
  167. "avg_number_of_sales_per_year": {
  168. "rate": {
  169. "field": "price", <2>
  170. "unit": "year", <3>
  171. "mode": "value_count" <4>
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. --------------------------------------------------
  179. // TEST[setup:sales]
  180. <1> Histogram is grouped by month.
  181. <2> Calculate number of of all sale prices
  182. <3> Convert to annual counts
  183. <4> Changing the mode to value count
  184. The response will contain the average daily sale prices for each month.
  185. [source,console-result]
  186. --------------------------------------------------
  187. {
  188. ...
  189. "aggregations" : {
  190. "by_date" : {
  191. "buckets" : [
  192. {
  193. "key_as_string" : "2015/01/01 00:00:00",
  194. "key" : 1420070400000,
  195. "doc_count" : 3,
  196. "avg_number_of_sales_per_year" : {
  197. "value" : 36.0
  198. }
  199. },
  200. {
  201. "key_as_string" : "2015/02/01 00:00:00",
  202. "key" : 1422748800000,
  203. "doc_count" : 2,
  204. "avg_number_of_sales_per_year" : {
  205. "value" : 24.0
  206. }
  207. },
  208. {
  209. "key_as_string" : "2015/03/01 00:00:00",
  210. "key" : 1425168000000,
  211. "doc_count" : 2,
  212. "avg_number_of_sales_per_year" : {
  213. "value" : 24.0
  214. }
  215. }
  216. ]
  217. }
  218. }
  219. }
  220. --------------------------------------------------
  221. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  222. By default `sum` mode is used.
  223. `"mode": "sum"`:: calculate the sum of all values field
  224. `"mode": "value_count"`:: use the number of values in the field
  225. The `mode` parameter can only be used with fields and scripts.
  226. ==== Relationship between bucket sizes and rate
  227. The `rate` aggregation supports all rate that can be used <<calendar_intervals,calendar_intervals parameter>> of `date_histogram`
  228. aggregation. The specified rate should compatible with the `date_histogram` aggregation interval, i.e. it should be possible to
  229. convert the bucket size into the rate. By default the interval of the `date_histogram` is used.
  230. `"rate": "second"`:: compatible with all intervals
  231. `"rate": "minute"`:: compatible with all intervals
  232. `"rate": "hour"`:: compatible with all intervals
  233. `"rate": "day"`:: compatible with all intervals
  234. `"rate": "week"`:: compatible with all intervals
  235. `"rate": "month"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  236. `"rate": "quarter"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  237. `"rate": "year"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  238. ==== Script
  239. The `rate` aggregation also supports scripting. For example, if we need to adjust out prices before calculating rates, we could use
  240. a script to recalculate them on-the-fly:
  241. [source,console]
  242. --------------------------------------------------
  243. GET sales/_search
  244. {
  245. "size": 0,
  246. "aggs": {
  247. "by_date": {
  248. "date_histogram": {
  249. "field": "date",
  250. "calendar_interval": "month"
  251. },
  252. "aggs": {
  253. "avg_price": {
  254. "rate": {
  255. "script": { <1>
  256. "lang": "painless",
  257. "source": "doc['price'].value * params.adjustment",
  258. "params": {
  259. "adjustment": 0.9 <2>
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. --------------------------------------------------
  269. // TEST[setup:sales]
  270. <1> The `field` parameter is replaced with a `script` parameter, which uses the
  271. script to generate values which percentiles are calculated on.
  272. <2> Scripting supports parameterized input just like any other script.
  273. [source,console-result]
  274. --------------------------------------------------
  275. {
  276. ...
  277. "aggregations" : {
  278. "by_date" : {
  279. "buckets" : [
  280. {
  281. "key_as_string" : "2015/01/01 00:00:00",
  282. "key" : 1420070400000,
  283. "doc_count" : 3,
  284. "avg_price" : {
  285. "value" : 495.0
  286. }
  287. },
  288. {
  289. "key_as_string" : "2015/02/01 00:00:00",
  290. "key" : 1422748800000,
  291. "doc_count" : 2,
  292. "avg_price" : {
  293. "value" : 54.0
  294. }
  295. },
  296. {
  297. "key_as_string" : "2015/03/01 00:00:00",
  298. "key" : 1425168000000,
  299. "doc_count" : 2,
  300. "avg_price" : {
  301. "value" : 337.5
  302. }
  303. }
  304. ]
  305. }
  306. }
  307. }
  308. --------------------------------------------------
  309. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]