rate-aggregation.asciidoc 9.2 KB

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