1
0

iprange-aggregation.asciidoc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [[java-aggs-bucket-iprange]]
  2. ==== Ip Range Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-bucket-iprange-aggregation.html[Ip Range Aggregation]
  5. with Java API.
  6. ===== Prepare aggregation request
  7. Here is an example on how to create the aggregation request:
  8. [source,java]
  9. --------------------------------------------------
  10. AggregationBuilder aggregation =
  11. AggregationBuilders
  12. .ipRange("agg")
  13. .field("ip")
  14. .addUnboundedTo("192.168.1.0") // from -infinity to 192.168.1.0 (excluded)
  15. .addRange("192.168.1.0", "192.168.2.0") // from 192.168.1.0 to 192.168.2.0 (excluded)
  16. .addUnboundedFrom("192.168.2.0"); // from 192.168.2.0 to +infinity
  17. --------------------------------------------------
  18. Note that you could also use ip masks as ranges:
  19. [source,java]
  20. --------------------------------------------------
  21. AggregationBuilder aggregation =
  22. AggregationBuilders
  23. .ipRange("agg")
  24. .field("ip")
  25. .addMaskRange("192.168.0.0/32")
  26. .addMaskRange("192.168.0.0/24")
  27. .addMaskRange("192.168.0.0/16");
  28. --------------------------------------------------
  29. ===== Use aggregation response
  30. Import Aggregation definition classes:
  31. [source,java]
  32. --------------------------------------------------
  33. import org.elasticsearch.search.aggregations.bucket.range.Range;
  34. --------------------------------------------------
  35. [source,java]
  36. --------------------------------------------------
  37. // sr is here your SearchResponse object
  38. Range agg = sr.getAggregations().get("agg");
  39. // For each entry
  40. for (Range.Bucket entry : agg.getBuckets()) {
  41. String key = entry.getKeyAsString(); // Ip range as key
  42. String fromAsString = entry.getFromAsString(); // Ip bucket from as a String
  43. String toAsString = entry.getToAsString(); // Ip bucket to as a String
  44. long docCount = entry.getDocCount(); // Doc count
  45. logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, fromAsString, toAsString, docCount);
  46. }
  47. --------------------------------------------------
  48. This will basically produce for the first example:
  49. [source,text]
  50. --------------------------------------------------
  51. key [*-192.168.1.0], from [null], to [192.168.1.0], doc_count [13]
  52. key [192.168.1.0-192.168.2.0], from [192.168.1.0], to [192.168.2.0], doc_count [14]
  53. key [192.168.2.0-*], from [192.168.2.0], to [null], doc_count [23]
  54. --------------------------------------------------
  55. And for the second one (using Ip masks):
  56. [source,text]
  57. --------------------------------------------------
  58. key [192.168.0.0/32], from [192.168.0.0], to [192.168.0.1], doc_count [0]
  59. key [192.168.0.0/24], from [192.168.0.0], to [192.168.1.0], doc_count [13]
  60. key [192.168.0.0/16], from [192.168.0.0], to [192.169.0.0], doc_count [50]
  61. --------------------------------------------------