💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# Percentiles Bucket Aggregation(百分数桶聚合) 同级管道聚合,它计算同级聚合中指定度量的所有桶的百分位数。指定度量必须是数字,并且同级聚合必须是多桶聚合。 ## Syntax(语法) 百分数聚合看起来像这样: | `{` `"percentiles_bucket"``: {` `"buckets_path"``: ``"the_sum"` `}` `}` |   ## Table 8. `sum_bucket` Parameters(Table 8\. sum_bucket参数) | 参数名称 | 描述 | 是否必要 | 默认值 | | --- | --- | --- | --- | | `buckets_path` | 我们希望找到的数据的桶的路径(相关详细信息,请参阅"buckets_path Syntax"一节) | 必须 |   | | `gap_policy` | 在数据中找到差异时应用的策略(相关详情,请参阅“Dealing with gaps in the data”一节) | 可选 | `skip` | | `format` | 应用于此聚合的输出值的格式 | 可选 | `null` | | `percents` | 要计算的百分数列表 | 可选 | `[ 1, 5, 25, 50, 75, 95, 99 ]` | 以下代码计算每月sales桶总数的百分数: | `POST /sales/_search` `{` `"size"``: ``0``,` `"aggs"` `: {` `"sales_per_month"` `: {` `"date_histogram"` `: {` `"field"` `: ``"date"``,` `"interval"` `: ``"month"` `},` `"aggs"``: {` `"sales"``: {` `"sum"``: {` `"field"``: ``"price"` `}` `}` `}` `},` `"percentiles_monthly_sales"``: {` `"percentiles_bucket"``: {` `"buckets_path"``: ``"sales_per_month>sales"``,   #``1` `"percents"``: [ ``25.0``, ``50.0``, ``75.0` `]           #``2` `}` `}` `}` `}` | | 1 | buckets_path指示这个为percentiles_bucket聚合,我们希望在sales_per_month数据直方图中计算sales聚合的统计数据。 | | 2 | percents指定了我们希望计算的百分数,在这里,是第25,第50和第75百分数。 | 以下是响应信息: | `{` `"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``,` `"sales"``: {` `"value"``: ``550.0` `}` `},` `{` `"key_as_string"``: ``"2015/02/01 00:00:00"``,` `"key"``: ``1422748800000``,` `"doc_count"``: ``2``,` `"sales"``: {` `"value"``: ``60.0` `}` `},` `{` `"key_as_string"``: ``"2015/03/01 00:00:00"``,` `"key"``: ``1425168000000``,` `"doc_count"``: ``2``,` `"sales"``: {` `"value"``: ``375.0` `}` `}` `]` `},` `"percentiles_monthly_sales"``: {` `"values"` `: {` `"25.0"``: ``375.0``,` `"50.0"``: ``375.0``,` `"75.0"``: ``550.0` `}` `}` `}` `}` | ## Percentiles_bucket implementation(Percentiles_bucket实现) 百分数桶返回最近输入的数据点,该数据点不大于所请求的百分数;它不会在数据点之间插值。 百分数是精确计算的,不是近似值(与百分数指标不同)。这意味着在丢弃数据之前,实现会在内存中维护一个有序的数据列表来计算百分数。如果你尝试在数百万的数据点中计算一个百分数的percentiles_bucket,可能会遇到内存压力问题。