bucket-script-aggregation.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. [[search-aggregations-pipeline-bucket-script-aggregation]]
  2. === Bucket Script Aggregation
  3. A parent pipeline aggregation which executes a script which can perform per bucket computations on specified metrics
  4. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a numeric value.
  5. ==== Syntax
  6. A `bucket_script` aggregation looks like this in isolation:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "bucket_script": {
  11. "buckets_path": {
  12. "my_var1": "the_sum", <1>
  13. "my_var2": "the_value_count"
  14. },
  15. "script": "params.my_var1 / params.my_var2"
  16. }
  17. }
  18. --------------------------------------------------
  19. // NOTCONSOLE
  20. <1> Here, `my_var1` is the name of the variable for this buckets path to use in the script, `the_sum` is the path to
  21. the metrics to use for that variable.
  22. .`bucket_script` Parameters
  23. |===
  24. |Parameter Name |Description |Required |Default Value
  25. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  26. for more details) |Required |
  27. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  28. (see <<buckets-path-syntax>> for more details) |Required |
  29. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  30. details)|Optional |`skip`
  31. |`format` |format to apply to the output value of this aggregation |Optional |`null`
  32. |===
  33. The following snippet calculates the ratio percentage of t-shirt sales compared to total sales each month:
  34. [source,js]
  35. --------------------------------------------------
  36. POST /sales/_search
  37. {
  38. "size": 0,
  39. "aggs" : {
  40. "sales_per_month" : {
  41. "date_histogram" : {
  42. "field" : "date",
  43. "interval" : "month"
  44. },
  45. "aggs": {
  46. "total_sales": {
  47. "sum": {
  48. "field": "price"
  49. }
  50. },
  51. "t-shirts": {
  52. "filter": {
  53. "term": {
  54. "type": "t-shirt"
  55. }
  56. },
  57. "aggs": {
  58. "sales": {
  59. "sum": {
  60. "field": "price"
  61. }
  62. }
  63. }
  64. },
  65. "t-shirt-percentage": {
  66. "bucket_script": {
  67. "buckets_path": {
  68. "tShirtSales": "t-shirts>sales",
  69. "totalSales": "total_sales"
  70. },
  71. "script": "params.tShirtSales / params.totalSales * 100"
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // CONSOLE
  80. // TEST[setup:sales]
  81. And the following may be the response:
  82. [source,js]
  83. --------------------------------------------------
  84. {
  85. "took": 11,
  86. "timed_out": false,
  87. "_shards": ...,
  88. "hits": ...,
  89. "aggregations": {
  90. "sales_per_month": {
  91. "buckets": [
  92. {
  93. "key_as_string": "2015/01/01 00:00:00",
  94. "key": 1420070400000,
  95. "doc_count": 3,
  96. "total_sales": {
  97. "value": 550.0
  98. },
  99. "t-shirts": {
  100. "doc_count": 1,
  101. "sales": {
  102. "value": 200.0
  103. }
  104. },
  105. "t-shirt-percentage": {
  106. "value": 36.36363636363637
  107. }
  108. },
  109. {
  110. "key_as_string": "2015/02/01 00:00:00",
  111. "key": 1422748800000,
  112. "doc_count": 2,
  113. "total_sales": {
  114. "value": 60.0
  115. },
  116. "t-shirts": {
  117. "doc_count": 1,
  118. "sales": {
  119. "value": 10.0
  120. }
  121. },
  122. "t-shirt-percentage": {
  123. "value": 16.666666666666664
  124. }
  125. },
  126. {
  127. "key_as_string": "2015/03/01 00:00:00",
  128. "key": 1425168000000,
  129. "doc_count": 2,
  130. "total_sales": {
  131. "value": 375.0
  132. },
  133. "t-shirts": {
  134. "doc_count": 1,
  135. "sales": {
  136. "value": 175.0
  137. }
  138. },
  139. "t-shirt-percentage": {
  140. "value": 46.666666666666664
  141. }
  142. }
  143. ]
  144. }
  145. }
  146. }
  147. --------------------------------------------------
  148. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  149. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  150. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]