IP Range Aggregation

Just like the dedicated date range aggregation, there is also a dedicated range aggregation for IP typed fields:

Example:

  1. GET /ip_addresses/_search
  2. {
  3. "size": 10,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "to": "10.0.0.5" },
  10. { "from": "10.0.0.5" }
  11. ]
  12. }
  13. }
  14. }
  15. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": [
  6. {
  7. "key": "*-10.0.0.5",
  8. "to": "10.0.0.5",
  9. "doc_count": 10
  10. },
  11. {
  12. "key": "10.0.0.5-*",
  13. "from": "10.0.0.5",
  14. "doc_count": 260
  15. }
  16. ]
  17. }
  18. }
  19. }

IP ranges can also be defined as CIDR masks:

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "mask": "10.0.0.0/25" },
  10. { "mask": "10.0.0.127/25" }
  11. ]
  12. }
  13. }
  14. }
  15. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": [
  6. {
  7. "key": "10.0.0.0/25",
  8. "from": "10.0.0.0",
  9. "to": "10.0.0.128",
  10. "doc_count": 128
  11. },
  12. {
  13. "key": "10.0.0.127/25",
  14. "from": "10.0.0.0",
  15. "to": "10.0.0.128",
  16. "doc_count": 128
  17. }
  18. ]
  19. }
  20. }
  21. }

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:

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "to": "10.0.0.5" },
  10. { "from": "10.0.0.5" }
  11. ],
  12. "keyed": true
  13. }
  14. }
  15. }
  16. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": {
  6. "*-10.0.0.5": {
  7. "to": "10.0.0.5",
  8. "doc_count": 10
  9. },
  10. "10.0.0.5-*": {
  11. "from": "10.0.0.5",
  12. "doc_count": 260
  13. }
  14. }
  15. }
  16. }
  17. }

It is also possible to customize the key for each range:

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "key": "infinity", "to": "10.0.0.5" },
  10. { "key": "and-beyond", "from": "10.0.0.5" }
  11. ],
  12. "keyed": true
  13. }
  14. }
  15. }
  16. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": {
  6. "infinity": {
  7. "to": "10.0.0.5",
  8. "doc_count": 10
  9. },
  10. "and-beyond": {
  11. "from": "10.0.0.5",
  12. "doc_count": 260
  13. }
  14. }
  15. }
  16. }
  17. }