2013-11-24 06:13:08 -05:00
[[search-aggregations-bucket-iprange-aggregation]]
2016-04-18 10:11:11 -04:00
=== IP Range Aggregation
2013-11-24 06:13:08 -05:00
2016-04-18 10:11:11 -04:00
Just like the dedicated <<search-aggregations-bucket-daterange-aggregation,date>> range aggregation, there is also a dedicated range aggregation for IP typed fields:
2013-11-24 06:13:08 -05:00
Example:
2019-09-05 10:11:25 -04:00
[source,console]
2013-11-24 06:13:08 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /ip_addresses/_search
2013-11-24 06:13:08 -05:00
{
2020-07-20 15:59:00 -04:00
"size": 10,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "ip",
"ranges": [
{ "to": "10.0.0.5" },
{ "from": "10.0.0.5" }
]
}
2013-11-24 06:13:08 -05:00
}
2020-07-20 15:59:00 -04:00
}
2013-11-24 06:13:08 -05:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TEST[setup:iprange]
2013-11-24 06:13:08 -05:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2013-11-24 06:13:08 -05:00
--------------------------------------------------
{
2020-07-20 15:59:00 -04:00
...
"aggregations": {
"ip_ranges": {
"buckets": [
{
"key": "*-10.0.0.5",
"to": "10.0.0.5",
"doc_count": 10
},
{
"key": "10.0.0.5-*",
"from": "10.0.0.5",
"doc_count": 260
2014-01-28 11:46:26 -05:00
}
2020-07-20 15:59:00 -04:00
]
2013-11-24 06:13:08 -05:00
}
2020-07-20 15:59:00 -04:00
}
2013-11-24 06:13:08 -05:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2013-11-24 06:13:08 -05:00
IP ranges can also be defined as CIDR masks:
2019-09-05 10:11:25 -04:00
[source,console]
2013-11-24 06:13:08 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /ip_addresses/_search
2013-11-24 06:13:08 -05:00
{
2020-07-20 15:59:00 -04:00
"size": 0,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "ip",
"ranges": [
{ "mask": "10.0.0.0/25" },
{ "mask": "10.0.0.127/25" }
]
}
2013-11-24 06:13:08 -05:00
}
2020-07-20 15:59:00 -04:00
}
2013-11-24 06:13:08 -05:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TEST[setup:iprange]
2013-11-24 06:13:08 -05:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2013-11-24 06:13:08 -05:00
--------------------------------------------------
{
2020-07-20 15:59:00 -04:00
...
"aggregations": {
"ip_ranges": {
"buckets": [
{
"key": "10.0.0.0/25",
"from": "10.0.0.0",
"to": "10.0.0.128",
"doc_count": 128
},
{
"key": "10.0.0.127/25",
"from": "10.0.0.0",
"to": "10.0.0.128",
"doc_count": 128
2014-01-28 11:46:26 -05:00
}
2020-07-20 15:59:00 -04:00
]
2013-11-24 06:13:08 -05:00
}
2020-07-20 15:59:00 -04:00
}
2013-11-24 06:13:08 -05:00
}
2016-04-18 10:11:11 -04:00
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2017-04-18 09:57:50 -04:00
==== Keyed Response
Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
2019-09-05 10:11:25 -04:00
[source,console]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /ip_addresses/_search
2017-04-18 09:57:50 -04:00
{
2020-07-20 15:59:00 -04:00
"size": 0,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "ip",
"ranges": [
{ "to": "10.0.0.5" },
{ "from": "10.0.0.5" }
],
"keyed": true
}
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TEST[setup:iprange]
2017-04-18 09:57:50 -04:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
{
2020-07-20 15:59:00 -04:00
...
"aggregations": {
"ip_ranges": {
"buckets": {
"*-10.0.0.5": {
"to": "10.0.0.5",
"doc_count": 10
},
"10.0.0.5-*": {
"from": "10.0.0.5",
"doc_count": 260
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2017-04-18 09:57:50 -04:00
It is also possible to customize the key for each range:
2019-09-05 10:11:25 -04:00
[source,console]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /ip_addresses/_search
2017-04-18 09:57:50 -04:00
{
2020-07-20 15:59:00 -04:00
"size": 0,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "ip",
"ranges": [
{ "key": "infinity", "to": "10.0.0.5" },
{ "key": "and-beyond", "from": "10.0.0.5" }
],
"keyed": true
}
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
--------------------------------------------------
2017-08-03 17:17:02 -04:00
// TEST[setup:iprange]
2017-04-18 09:57:50 -04:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
{
2020-07-20 15:59:00 -04:00
...
"aggregations": {
"ip_ranges": {
"buckets": {
"infinity": {
"to": "10.0.0.5",
"doc_count": 10
},
"and-beyond": {
"from": "10.0.0.5",
"doc_count": 260
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-04-18 09:57:50 -04:00
}
--------------------------------------------------
2017-12-14 11:47:53 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]