rate-aggregation.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. [role="xpack"]
  2. [[search-aggregations-metrics-rate-aggregation]]
  3. === Rate aggregation
  4. ++++
  5. <titleabbrev>Rate</titleabbrev>
  6. ++++
  7. A `rate` metrics aggregation can be used only inside a `date_histogram` or `composite` aggregation. It calculates a rate of documents
  8. or a field in each bucket. The field values can be extracted from specific numeric or
  9. <<histogram,histogram fields>> in the documents.
  10. NOTE: For `composite` aggregations, there must be exactly one `date_histogram` source for the `rate` aggregation to be supported.
  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 buckets and then convert the number of sales transactions 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 transactions in each bucket. Since there are 12 months per year, the annual rate will
  51. be automatically calculated by multiplying the 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. You can also take advantage of `composite` aggregations to calculate the average daily sale price for each item in
  158. your inventory
  159. [source,console]
  160. --------------------------------------------------
  161. GET sales/_search?filter_path=aggregations&size=0
  162. {
  163. "aggs": {
  164. "buckets": {
  165. "composite": { <1>
  166. "sources": [
  167. {
  168. "month": {
  169. "date_histogram": { <2>
  170. "field": "date",
  171. "calendar_interval": "month"
  172. }
  173. }
  174. },
  175. {
  176. "type": { <3>
  177. "terms": {
  178. "field": "type"
  179. }
  180. }
  181. }
  182. ]
  183. },
  184. "aggs": {
  185. "avg_price": {
  186. "rate": {
  187. "field": "price", <4>
  188. "unit": "day" <5>
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. --------------------------------------------------
  196. // TEST[setup:sales]
  197. <1> Composite aggregation with a date histogram source
  198. and a source for the item type.
  199. <2> The date histogram source grouping monthly
  200. <3> The terms source grouping for each sale item type
  201. <4> Calculate sum of all sale prices, per month and item
  202. <5> Convert to average daily sales per item
  203. The response will contain the average daily sale prices for each month per item.
  204. [source,console-result]
  205. --------------------------------------------------
  206. {
  207. "aggregations" : {
  208. "buckets" : {
  209. "after_key" : {
  210. "month" : 1425168000000,
  211. "type" : "t-shirt"
  212. },
  213. "buckets" : [
  214. {
  215. "key" : {
  216. "month" : 1420070400000,
  217. "type" : "bag"
  218. },
  219. "doc_count" : 1,
  220. "avg_price" : {
  221. "value" : 4.838709677419355
  222. }
  223. },
  224. {
  225. "key" : {
  226. "month" : 1420070400000,
  227. "type" : "hat"
  228. },
  229. "doc_count" : 1,
  230. "avg_price" : {
  231. "value" : 6.451612903225806
  232. }
  233. },
  234. {
  235. "key" : {
  236. "month" : 1420070400000,
  237. "type" : "t-shirt"
  238. },
  239. "doc_count" : 1,
  240. "avg_price" : {
  241. "value" : 6.451612903225806
  242. }
  243. },
  244. {
  245. "key" : {
  246. "month" : 1422748800000,
  247. "type" : "hat"
  248. },
  249. "doc_count" : 1,
  250. "avg_price" : {
  251. "value" : 1.7857142857142858
  252. }
  253. },
  254. {
  255. "key" : {
  256. "month" : 1422748800000,
  257. "type" : "t-shirt"
  258. },
  259. "doc_count" : 1,
  260. "avg_price" : {
  261. "value" : 0.35714285714285715
  262. }
  263. },
  264. {
  265. "key" : {
  266. "month" : 1425168000000,
  267. "type" : "hat"
  268. },
  269. "doc_count" : 1,
  270. "avg_price" : {
  271. "value" : 6.451612903225806
  272. }
  273. },
  274. {
  275. "key" : {
  276. "month" : 1425168000000,
  277. "type" : "t-shirt"
  278. },
  279. "doc_count" : 1,
  280. "avg_price" : {
  281. "value" : 5.645161290322581
  282. }
  283. }
  284. ]
  285. }
  286. }
  287. }
  288. --------------------------------------------------
  289. 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:
  290. [source,console]
  291. --------------------------------------------------
  292. GET sales/_search
  293. {
  294. "size": 0,
  295. "aggs": {
  296. "by_date": {
  297. "date_histogram": {
  298. "field": "date",
  299. "calendar_interval": "month" <1>
  300. },
  301. "aggs": {
  302. "avg_number_of_sales_per_year": {
  303. "rate": {
  304. "field": "price", <2>
  305. "unit": "year", <3>
  306. "mode": "value_count" <4>
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. --------------------------------------------------
  314. // TEST[setup:sales]
  315. <1> Histogram is grouped by month.
  316. <2> Calculate number of all sale prices
  317. <3> Convert to annual counts
  318. <4> Changing the mode to value count
  319. The response will contain the average daily sale prices for each month.
  320. [source,console-result]
  321. --------------------------------------------------
  322. {
  323. ...
  324. "aggregations" : {
  325. "by_date" : {
  326. "buckets" : [
  327. {
  328. "key_as_string" : "2015/01/01 00:00:00",
  329. "key" : 1420070400000,
  330. "doc_count" : 3,
  331. "avg_number_of_sales_per_year" : {
  332. "value" : 36.0
  333. }
  334. },
  335. {
  336. "key_as_string" : "2015/02/01 00:00:00",
  337. "key" : 1422748800000,
  338. "doc_count" : 2,
  339. "avg_number_of_sales_per_year" : {
  340. "value" : 24.0
  341. }
  342. },
  343. {
  344. "key_as_string" : "2015/03/01 00:00:00",
  345. "key" : 1425168000000,
  346. "doc_count" : 2,
  347. "avg_number_of_sales_per_year" : {
  348. "value" : 24.0
  349. }
  350. }
  351. ]
  352. }
  353. }
  354. }
  355. --------------------------------------------------
  356. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  357. By default `sum` mode is used.
  358. `"mode": "sum"`:: calculate the sum of all values field
  359. `"mode": "value_count"`:: use the number of values in the field
  360. ==== Relationship between bucket sizes and rate
  361. The `rate` aggregation supports all rate that can be used <<calendar_intervals,calendar_intervals parameter>> of `date_histogram`
  362. aggregation. The specified rate should compatible with the `date_histogram` aggregation interval, i.e. it should be possible to
  363. convert the bucket size into the rate. By default the interval of the `date_histogram` is used.
  364. `"rate": "second"`:: compatible with all intervals
  365. `"rate": "minute"`:: compatible with all intervals
  366. `"rate": "hour"`:: compatible with all intervals
  367. `"rate": "day"`:: compatible with all intervals
  368. `"rate": "week"`:: compatible with all intervals
  369. `"rate": "month"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  370. `"rate": "quarter"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  371. `"rate": "year"`:: compatible with only with `month`, `quarter` and `year` calendar intervals
  372. There is also an additional limitations if the date histogram is not a direct parent of the rate histogram. In this case both rate interval
  373. and histogram interval have to be in the same group: [`second`, ` minute`, `hour`, `day`, `week`] or [`month`, `quarter`, `year`]. For
  374. example, if the date histogram is `month` based, only rate intervals of `month`, `quarter` or `year` are supported. If the date histogram
  375. is `day` based, only `second`, ` minute`, `hour`, `day`, and `week` rate intervals are supported.
  376. ==== Script
  377. If you need to run the aggregation against values that aren't indexed, run the
  378. aggregation on a <<runtime,runtime field>>. For example, if we need to adjust
  379. our prices before calculating rates:
  380. [source,console]
  381. ----
  382. GET sales/_search
  383. {
  384. "size": 0,
  385. "runtime_mappings": {
  386. "price.adjusted": {
  387. "type": "double",
  388. "script": {
  389. "source": "emit(doc['price'].value * params.adjustment)",
  390. "params": {
  391. "adjustment": 0.9
  392. }
  393. }
  394. }
  395. },
  396. "aggs": {
  397. "by_date": {
  398. "date_histogram": {
  399. "field": "date",
  400. "calendar_interval": "month"
  401. },
  402. "aggs": {
  403. "avg_price": {
  404. "rate": {
  405. "field": "price.adjusted"
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. ----
  413. // TEST[setup:sales]
  414. [source,console-result]
  415. ----
  416. {
  417. ...
  418. "aggregations" : {
  419. "by_date" : {
  420. "buckets" : [
  421. {
  422. "key_as_string" : "2015/01/01 00:00:00",
  423. "key" : 1420070400000,
  424. "doc_count" : 3,
  425. "avg_price" : {
  426. "value" : 495.0
  427. }
  428. },
  429. {
  430. "key_as_string" : "2015/02/01 00:00:00",
  431. "key" : 1422748800000,
  432. "doc_count" : 2,
  433. "avg_price" : {
  434. "value" : 54.0
  435. }
  436. },
  437. {
  438. "key_as_string" : "2015/03/01 00:00:00",
  439. "key" : 1425168000000,
  440. "doc_count" : 2,
  441. "avg_price" : {
  442. "value" : 337.5
  443. }
  444. }
  445. ]
  446. }
  447. }
  448. }
  449. ----
  450. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]