bucket-selector-aggregation.asciidoc 3.5 KB

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