1
0

bucket-script-aggregation.asciidoc 4.7 KB

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