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
{
2017-08-03 17:17:02 -04:00
"size": 10,
2013-11-24 06:13:08 -05:00
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [
{ "to" : "10.0.0.5" },
{ "from" : "10.0.0.5" }
]
}
}
}
}
--------------------------------------------------
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
--------------------------------------------------
{
...
"aggregations": {
2015-02-21 03:42:37 -05:00
"ip_ranges": {
2014-01-28 11:46:26 -05:00
"buckets" : [
{
2018-05-24 11:55:14 -04:00
"key": "*-10.0.0.5",
2016-04-18 10:11:11 -04:00
"to": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 10
2014-01-28 11:46:26 -05:00
},
{
2018-05-24 11:55:14 -04:00
"key": "10.0.0.5-*",
2016-04-18 10:11:11 -04:00
"from": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 260
2014-01-28 11:46:26 -05: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
{
2017-08-03 17:17:02 -04:00
"size": 0,
2013-11-24 06:13:08 -05:00
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [
{ "mask" : "10.0.0.0/25" },
{ "mask" : "10.0.0.127/25" }
]
}
}
}
}
--------------------------------------------------
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
--------------------------------------------------
{
2017-08-03 17:17:02 -04:00
...
2013-11-24 06:13:08 -05:00
"aggregations": {
2014-01-28 11:46:26 -05:00
"ip_ranges": {
"buckets": [
{
"key": "10.0.0.0/25",
2016-04-18 10:11:11 -04:00
"from": "10.0.0.0",
2017-08-03 17:17:02 -04:00
"to": "10.0.0.128",
"doc_count": 128
2014-01-28 11:46:26 -05:00
},
{
"key": "10.0.0.127/25",
2016-04-18 10:11:11 -04:00
"from": "10.0.0.0",
2017-08-03 17:17:02 -04:00
"to": "10.0.0.128",
"doc_count": 128
2014-01-28 11:46:26 -05: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
{
2017-08-03 17:17:02 -04:00
"size": 0,
2017-04-18 09:57:50 -04:00
"aggs": {
"ip_ranges": {
"ip_range": {
2017-08-03 17:17:02 -04:00
"field": "ip",
2017-04-18 09:57:50 -04:00
"ranges": [
{ "to" : "10.0.0.5" },
{ "from" : "10.0.0.5" }
],
"keyed": true
}
}
}
}
--------------------------------------------------
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
--------------------------------------------------
{
...
"aggregations": {
"ip_ranges": {
"buckets": {
"*-10.0.0.5": {
"to": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 10
2017-04-18 09:57:50 -04:00
},
"10.0.0.5-*": {
"from": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 260
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
{
2017-08-03 17:17:02 -04:00
"size": 0,
2017-04-18 09:57:50 -04:00
"aggs": {
"ip_ranges": {
"ip_range": {
2017-08-03 17:17:02 -04:00
"field": "ip",
2017-04-18 09:57:50 -04:00
"ranges": [
{ "key": "infinity", "to" : "10.0.0.5" },
{ "key": "and-beyond", "from" : "10.0.0.5" }
],
"keyed": true
}
}
}
}
--------------------------------------------------
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
--------------------------------------------------
{
...
"aggregations": {
"ip_ranges": {
"buckets": {
"infinity": {
"to": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 10
2017-04-18 09:57:50 -04:00
},
"and-beyond": {
"from": "10.0.0.5",
2017-08-03 17:17:02 -04:00
"doc_count": 260
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,/]