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:
[source,js]
--------------------------------------------------
2017-08-03 17:17:02 -04:00
GET /ip_addresses/data/_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
// CONSOLE
// TEST[setup:iprange]
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
2015-02-21 03:42:37 -05:00
"ip_ranges": {
2014-01-28 11:46:26 -05:00
"buckets" : [
{
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
},
{
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:
[source,js]
--------------------------------------------------
2017-08-03 17:17:02 -04:00
GET /ip_addresses/data/_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
// CONSOLE
// TEST[setup:iprange]
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
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:
[source,js]
--------------------------------------------------
2017-08-03 17:17:02 -04:00
GET /ip_addresses/data/_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
// CONSOLE
// TEST[setup:iprange]
2017-04-18 09:57:50 -04:00
Response:
[source,js]
--------------------------------------------------
{
...
"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:
[source,js]
--------------------------------------------------
2017-08-03 17:17:02 -04:00
GET /ip_addresses/data/_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
// CONSOLE
// TEST[setup:iprange]
2017-04-18 09:57:50 -04:00
Response:
[source,js]
--------------------------------------------------
{
...
"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-08-03 17:17:02 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]