1
0

bucket-selector-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. [[search-aggregations-pipeline-bucket-selector-aggregation]]
  2. === Bucket Selector Aggregation
  3. A parent pipeline aggregation which executes a script which determines whether the current bucket will be retained
  4. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a boolean value.
  5. If the script language is `expression` then a numeric return value is permitted. In this case 0.0 will be evaluated as `false`
  6. and all other values will evaluate to true.
  7. NOTE: The bucket_selector aggregation, like all pipeline aggregations, executes after all other sibling aggregations. This means that
  8. using the bucket_selector aggregation to filter the returned buckets in the response does not save on execution time running the aggregations.
  9. ==== Syntax
  10. A `bucket_selector` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "bucket_selector": {
  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_selector` Parameters
  27. |===
  28. |Parameter Name |Description |Required |Default Value
  29. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  30. for more details) |Required |
  31. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  32. (see <<buckets-path-syntax>> for more details) |Required |
  33. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  34. details)|Optional |`skip`
  35. |===
  36. The following snippet only retains buckets where the total sales for the month is more than 200:
  37. [source,js]
  38. --------------------------------------------------
  39. POST /sales/_search
  40. {
  41. "size": 0,
  42. "aggs" : {
  43. "sales_per_month" : {
  44. "date_histogram" : {
  45. "field" : "date",
  46. "interval" : "month"
  47. },
  48. "aggs": {
  49. "total_sales": {
  50. "sum": {
  51. "field": "price"
  52. }
  53. },
  54. "sales_bucket_filter": {
  55. "bucket_selector": {
  56. "buckets_path": {
  57. "totalSales": "total_sales"
  58. },
  59. "script": "params.totalSales > 200"
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. // CONSOLE
  68. // TEST[setup:sales]
  69. And the following may be the response:
  70. [source,js]
  71. --------------------------------------------------
  72. {
  73. "took": 11,
  74. "timed_out": false,
  75. "_shards": ...,
  76. "hits": ...,
  77. "aggregations": {
  78. "sales_per_month": {
  79. "buckets": [
  80. {
  81. "key_as_string": "2015/01/01 00:00:00",
  82. "key": 1420070400000,
  83. "doc_count": 3,
  84. "total_sales": {
  85. "value": 550.0
  86. }
  87. },<1>
  88. {
  89. "key_as_string": "2015/03/01 00:00:00",
  90. "key": 1425168000000,
  91. "doc_count": 2,
  92. "total_sales": {
  93. "value": 375.0
  94. },
  95. }
  96. ]
  97. }
  98. }
  99. }
  100. --------------------------------------------------
  101. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  102. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  103. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  104. <1> Bucket for `2015/02/01 00:00:00` has been removed as its total sales was less than 200