bucket-script-aggregation.asciidoc 4.9 KB

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