# 范围聚合(Range Aggregation)

A multi-bucket value source based aggregation that enables the user to define a set of ranges－每个代表一个bucket。在聚合过程中，从每个文件中提取的值将根据每个存储区范围进行检查，并对相关/匹配文档进行“bucket”，值得注意的是，注意，此聚合包含了from值，不包括每个范围的to的值。

例子：

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            }
        }
    }
}
```

响应结果：

```
{
    ...

    "aggregations": {
        "price_ranges" : {
            "buckets": [
                {
                    "to": 50,
                    "doc_count": 2
                },
                {
                    "from": 50,
                    "to": 100,
                    "doc_count": 4
                },
                {
                    "from": 100,
                    "doc_count": 4
                }
            ]
        }
    }
}
```

## Keyed Response <a href="#rangeaggregation-fan-wei-ju-he-keyedresponse" id="rangeaggregation-fan-wei-ju-he-keyedresponse"></a>

将keyed标志设置为true会将一个惟一的字符串键与每个bucket关联起来，并将范围作为散列而不是数组返回:

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "keyed" : true,
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            }
        }
    }
}
```

响应结果：

```
{
    ...

    "aggregations": {
        "price_ranges" : {
            "buckets": {
                "*-50.0": {
                    "to": 50,
                    "doc_count": 2
                },
                "50.0-100.0": {
                    "from": 50,
                    "to": 100,
                    "doc_count": 4
                },
                "100.0-*": {
                    "from": 100,
                    "doc_count": 4
                }
            }
        }
    }
}
```

可以为每个范围自定义key：

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "keyed" : true,
                "ranges" : [
                    { "key" : "cheap", "to" : 50 },
                    { "key" : "average", "from" : 50, "to" : 100 },
                    { "key" : "expensive", "from" : 100 }
                ]
            }
        }
    }
}
```

## Script <a href="#rangeaggregation-fan-wei-ju-he-script" id="rangeaggregation-fan-wei-ju-he-script"></a>

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "script" : {
                    "lang": "painless",
                    "inline": "doc['price'].value"
                },
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            }
        }
    }
}
```

这将把script参数解释为具有painless脚本语言和no script参数的inline脚本。要使用文件脚本，请使用以下语法

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "script" : {
                    "file": "my_script",
                    "params": {
                        "field": "price"
                    }
                },
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            }
        }
    }
}
```

> 对于索引脚本，用id参数替换file参数。

## Value Script <a href="#rangeaggregation-fan-wei-ju-he-valuescript" id="rangeaggregation-fan-wei-ju-he-valuescript"></a>

假设产品价格是美元，但我们希望得到欧元的价格区间，我们可以使用value script在聚合之前转换价格（假设汇率是0.8）

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "script" : {
                    "lang": "painless",
                    "inline": "_value * params.conversion_rate",
                    "params" : {
                        "conversion_rate" : 0.8
                    }
                },
                "ranges" : [
                    { "to" : 35 },
                    { "from" : 35, "to" : 70 },
                    { "from" : 70 }
                ]
            }
        }
    }
}
```

## Sub Aggregations <a href="#rangeaggregation-fan-wei-ju-he-subaggregations" id="rangeaggregation-fan-wei-ju-he-subaggregations"></a>

下面的示例不仅将文档“bucket”到不同的bucket，而且计算每个价格范围内的价格统计

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            },
            "aggs" : {
                "price_stats" : {
                    "stats" : { "field" : "price" }
                }
            }
        }
    }
}
```

响应结果：

```
{
    "aggregations": {
        "price_ranges" : {
            "buckets": [
                {
                    "to": 50,
                    "doc_count": 2,
                    "price_stats": {
                        "count": 2,
                        "min": 20,
                        "max": 47,
                        "avg": 33.5,
                        "sum": 67
                    }
                },
                {
                    "from": 50,
                    "to": 100,
                    "doc_count": 4,
                    "price_stats": {
                        "count": 4,
                        "min": 60,
                        "max": 98,
                        "avg": 82.5,
                        "sum": 330
                    }
                },
                {
                    "from": 100,
                    "doc_count": 4,
                    "price_stats": {
                        "count": 4,
                        "min": 134,
                        "max": 367,
                        "avg": 216,
                        "sum": 864
                    }
                }
            ]
        }
    }
}
```

如果sub aggregation（子集合）也基于与范围聚合相同的值（如上例中的统计信息聚合），则可以省略其值的定义。以下内容将返回与上述相同的结果

```
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 50, "to" : 100 },
                    { "from" : 100 }
                ]
            },
            "aggs" : {
                "price_stats" : {
                    "stats" : {} ＃1
                }
            }
        }
    }
}
```

＃1 我们不需要指定price，因为我们默认从父范围聚合“继承”它


---

# 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/362tong-ju540828-bucketaggregations/fan-wei-ju-540828-range-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.
