1
0

bucket-script-aggregation.asciidoc 4.5 KB

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