rate-aggregation.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.
  7. ==== Syntax
  8. A `rate` aggregation looks like this in isolation:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "rate": {
  13. "unit": "month",
  14. "field": "requests"
  15. }
  16. }
  17. --------------------------------------------------
  18. // NOTCONSOLE
  19. The following request will group all sales records into monthly bucket and than convert the number of sales transaction in each bucket
  20. into per annual sales rate.
  21. [source,console]
  22. --------------------------------------------------
  23. GET sales/_search
  24. {
  25. "size": 0,
  26. "aggs": {
  27. "by_date": {
  28. "date_histogram": {
  29. "field": "date",
  30. "calendar_interval": "month" <1>
  31. },
  32. "aggs": {
  33. "my_rate": {
  34. "rate": {
  35. "unit": "year" <2>
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. // TEST[setup:sales]
  44. <1> Histogram is grouped by month.
  45. <2> But the rate is converted into annual rate.
  46. The response will return the annual rate of transaction in each bucket. Since there are 12 months per year, the annual rate will
  47. be automatically calculated by multiplying monthly rate by 12.
  48. [source,console-result]
  49. --------------------------------------------------
  50. {
  51. ...
  52. "aggregations" : {
  53. "by_date" : {
  54. "buckets" : [
  55. {
  56. "key_as_string" : "2015/01/01 00:00:00",
  57. "key" : 1420070400000,
  58. "doc_count" : 3,
  59. "my_rate" : {
  60. "value" : 36.0
  61. }
  62. },
  63. {
  64. "key_as_string" : "2015/02/01 00:00:00",
  65. "key" : 1422748800000,
  66. "doc_count" : 2,
  67. "my_rate" : {
  68. "value" : 24.0
  69. }
  70. },
  71. {
  72. "key_as_string" : "2015/03/01 00:00:00",
  73. "key" : 1425168000000,
  74. "doc_count" : 2,
  75. "my_rate" : {
  76. "value" : 24.0
  77. }
  78. }
  79. ]
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  85. 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
  86. bucket. The following request will group all sales records into monthly bucket and than calculate the total monthly sales and convert them
  87. into average daily sales.
  88. [source,console]
  89. --------------------------------------------------
  90. GET sales/_search
  91. {
  92. "size": 0,
  93. "aggs": {
  94. "by_date": {
  95. "date_histogram": {
  96. "field": "date",
  97. "calendar_interval": "month" <1>
  98. },
  99. "aggs": {
  100. "avg_price": {
  101. "rate": {
  102. "field": "price", <2>
  103. "unit": "day" <3>
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. // TEST[setup:sales]
  112. <1> Histogram is grouped by month.
  113. <2> Calculate sum of all sale prices
  114. <3> Convert to average daily sales
  115. The response will contain the average daily sale prices for each month.
  116. [source,console-result]
  117. --------------------------------------------------
  118. {
  119. ...
  120. "aggregations" : {
  121. "by_date" : {
  122. "buckets" : [
  123. {
  124. "key_as_string" : "2015/01/01 00:00:00",
  125. "key" : 1420070400000,
  126. "doc_count" : 3,
  127. "avg_price" : {
  128. "value" : 17.741935483870968
  129. }
  130. },
  131. {
  132. "key_as_string" : "2015/02/01 00:00:00",
  133. "key" : 1422748800000,
  134. "doc_count" : 2,
  135. "avg_price" : {
  136. "value" : 2.142857142857143
  137. }
  138. },
  139. {
  140. "key_as_string" : "2015/03/01 00:00:00",
  141. "key" : 1425168000000,
  142. "doc_count" : 2,
  143. "avg_price" : {
  144. "value" : 12.096774193548388
  145. }
  146. }
  147. ]
  148. }
  149. }
  150. }
  151. --------------------------------------------------
  152. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  153. ==== Relationship between bucket sizes and rate
  154. The `rate` aggregation supports all rate that can be used <<calendar_intervals,calendar_intervals parameter>> of `date_histogram`
  155. aggregation. The specified rate should compatible with the `date_histogram` aggregation interval, i.e. it should be possible to
  156. convert the bucket size into the rate. By default the interval of the `date_histogram` is used.
  157. `"rate": "second"`:: compatible with all intervals
  158. `"rate": "minute"`:: compatible with all intervals
  159. `"rate": "hour"`:: compatible with all intervals
  160. `"rate": "day"`:: compatible with all intervals
  161. `"rate": "week"`:: compatible with all intervals
  162. `"rate": "month"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  163. `"rate": "quarter"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  164. `"rate": "year"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  165. ==== Script
  166. The `rate` aggregation also supports scripting. For example, if we need to adjust out prices before calculating rates, we could use
  167. a script to recalculate them on-the-fly:
  168. [source,console]
  169. --------------------------------------------------
  170. GET sales/_search
  171. {
  172. "size": 0,
  173. "aggs": {
  174. "by_date": {
  175. "date_histogram": {
  176. "field": "date",
  177. "calendar_interval": "month"
  178. },
  179. "aggs": {
  180. "avg_price": {
  181. "rate": {
  182. "script": { <1>
  183. "lang": "painless",
  184. "source": "doc['price'].value * params.adjustment",
  185. "params": {
  186. "adjustment": 0.9 <2>
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. --------------------------------------------------
  196. // TEST[setup:sales]
  197. <1> The `field` parameter is replaced with a `script` parameter, which uses the
  198. script to generate values which percentiles are calculated on.
  199. <2> Scripting supports parameterized input just like any other script.
  200. [source,console-result]
  201. --------------------------------------------------
  202. {
  203. ...
  204. "aggregations" : {
  205. "by_date" : {
  206. "buckets" : [
  207. {
  208. "key_as_string" : "2015/01/01 00:00:00",
  209. "key" : 1420070400000,
  210. "doc_count" : 3,
  211. "avg_price" : {
  212. "value" : 495.0
  213. }
  214. },
  215. {
  216. "key_as_string" : "2015/02/01 00:00:00",
  217. "key" : 1422748800000,
  218. "doc_count" : 2,
  219. "avg_price" : {
  220. "value" : 54.0
  221. }
  222. },
  223. {
  224. "key_as_string" : "2015/03/01 00:00:00",
  225. "key" : 1425168000000,
  226. "doc_count" : 2,
  227. "avg_price" : {
  228. "value" : 337.5
  229. }
  230. }
  231. ]
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]