# IP范围聚合(IP Range Aggregation)

就像专用的[日期](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html)范围聚合一样，IP类型字段也有专用的范围聚合:

例子：

```
{
    "aggs" : {
        "ip_ranges" : {
            "ip_range" : {
                "field" : "ip",
                "ranges" : [
                    { "to" : "10.0.0.5" },
                    { "from" : "10.0.0.5" }
                ]
            }
        }
    }
}
```

响应结果：

```
{
    ...

    "aggregations": {
        "ip_ranges": {
            "buckets" : [
                {
                    "to": "10.0.0.5",
                    "doc_count": 4
                },
                {
                    "from": "10.0.0.5",
                    "doc_count": 6
                }
            ]
        }
    }
}
```

IP范围也可以定义为CIDR掩码

```
{
    "aggs" : {
        "ip_ranges" : {
            "ip_range" : {
                "field" : "ip",
                "ranges" : [
                    { "mask" : "10.0.0.0/25" },
                    { "mask" : "10.0.0.127/25" }
                ]
            }
        }
    }
}
```

响应结果：

```
{
    "aggregations": {
        "ip_ranges": {
            "buckets": [
                {
                    "key": "10.0.0.0/25",
                    "from": "10.0.0.0",
                    "to": "10.0.0.127",
                    "doc_count": 127
                },
                {
                    "key": "10.0.0.127/25",
                    "from": "10.0.0.0",
                    "to": "10.0.0.127",
                    "doc_count": 127
                }
            ]
        }
    }
}
```

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

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

```
{
    "aggs": {
        "ip_ranges": {
            "ip_range": {
                "field": "remote_ip",
                "ranges": [
                    { "to" : "10.0.0.5" },
                    { "from" : "10.0.0.5" }
                ],
                "keyed": true
            }
        }
    }
}
```

响应结果：

```
{
    ...

    "aggregations": {
        "ip_ranges": {
            "buckets": {
                "*-10.0.0.5": {
                    "to": "10.0.0.5",
                    "doc_count": 1462
                },
                "10.0.0.5-*": {
                    "from": "10.0.0.5",
                    "doc_count": 50000
                }
            }
        }
    }
}
```

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

```
{
    "aggs": {
        "ip_ranges": {
            "ip_range": {
                "field": "remote_ip",
                "ranges": [
                    { "key": "infinity", "to" : "10.0.0.5" },
                    { "key": "and-beyond", "from" : "10.0.0.5" }
                ],
                "keyed": true
            }
        }
    }
}
```

响应结果：

```
{
    ...

    "aggregations": {
        "ip_ranges": {
            "buckets": {
                "infinity": {
                    "to": "10.0.0.5",
                    "doc_count": 1462
                },
                "and-beyond": {
                    "from": "10.0.0.5",
                    "doc_count": 50000
                }
            }
        }
    }
}
```


---

# 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/ipfan-wei-ju-540828-ip-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.
