# 桶选择器聚合(Bucket Selector Aggregation)

> 警告：此功能是实验性的，可能会在未来的版本中完全更改或删除。Elastic将采取最大的努力来解决任何问题，但实验功能不受SLA官方功能的支持。

父管道聚合，它执行一个脚本，该脚本确定当前桶是否将保留在父多桶聚合中。指定度量必须是数字，脚本必须返回一个布尔值。如果脚本语言是表达式，则允许数字返回值。在这种情况下，0.0将被判定为false，所有的其他值将被判定为true。

注意：bucket\_selector聚合，像所有管道聚合，在所有的其他同级聚合后执行。这意味着使用bucket\_selector聚合过滤返回的响应数据中不会保存运行时的聚合。

### Syntax（语法）

bucket\_selector聚合看起来像这样：

```
{
    "bucket_selector": {
        "buckets_path": {
            "my_var1": "the_sum",                     #1
            "my_var2": "the_value_count"
        },
        "script": "params.my_var1 > params.my_var2"
    }
}
```

这里，my\_var1是这个桶的路径中使用脚本中的变量的名称，the\_sum是路径的度量所使用的变量。

## bucket\_selector参数 <a href="#bucketselectoraggregation-tong-xuan-ze-qi-ju-he-table12.bucketselectorparameterstable12.bucketselect" id="bucketselectoraggregation-tong-xuan-ze-qi-ju-he-table12.bucketselectorparameterstable12.bucketselect"></a>

| 参数名称           | 描述                                                            | 是否必要 | 默认值    |
| -------------- | ------------------------------------------------------------- | ---- | ------ |
| `script`       | 在这个聚合上运行的脚本。这个脚本可以是内置的，文件或索引。（详细信息，请参阅Scripting）              | 必须   |        |
| `buckets_path` | 脚本变量的映射，以及我们希望用于桶的变量的相关路径（相关详细信息，请参阅“buckets\_path Syntax”一节） | 必须   |        |
| `gap_policy`   | 在数据中找到差异时应用的策略（相关详情：请参阅“Dealing with gaps in the data”一节）     | 可选   | `skip` |

以下代码仅保留了该月份的总销售额超过400的数据桶：

```
POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "sales_bucket_filter": {
                    "bucket_selector": {
                        "buckets_path": {
                          "totalSales": "total_sales"
                        },
                        "script": "params.totalSales > 200"
                    }
                }
            }
        }
    }
}
```

以下是响应信息：

```
{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "total_sales": {
                   "value": 550.0
               }
            },                                            #1
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "total_sales": {
                   "value": 375.0
               },
            }
         ]
      }
   }
}
```

由于其总销售额不足200，所以2015/02/01 00:00:00 的bucket已被移除


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/363guan-dao-ju-540828-pipeline-aggregations/tong-xuan-zeqi-ju-540828-bucketselector-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
