bucket-selector-aggregation.asciidoc 3.7 KB

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