geohashgrid-aggregation.asciidoc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. [[java-aggs-bucket-geohashgrid]]
  2. ==== Geo Hash Grid Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-bucket-geohashgrid-aggregation.html[Geo Hash Grid 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. .geohashGrid("agg")
  13. .field("address.location")
  14. .precision(4);
  15. --------------------------------------------------
  16. ===== Use aggregation response
  17. Import Aggregation definition classes:
  18. [source,java]
  19. --------------------------------------------------
  20. import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
  21. --------------------------------------------------
  22. [source,java]
  23. --------------------------------------------------
  24. // sr is here your SearchResponse object
  25. GeoHashGrid agg = sr.getAggregations().get("agg");
  26. // For each entry
  27. for (GeoHashGrid.Bucket entry : agg.getBuckets()) {
  28. String keyAsString = entry.getKeyAsString(); // key as String
  29. GeoPoint key = (GeoPoint) entry.getKey(); // key as geo point
  30. long docCount = entry.getDocCount(); // Doc count
  31. logger.info("key [{}], point {}, doc_count [{}]", keyAsString, key, docCount);
  32. }
  33. --------------------------------------------------
  34. This will basically produce:
  35. [source,text]
  36. --------------------------------------------------
  37. key [gbqu], point [47.197265625, -1.58203125], doc_count [1282]
  38. key [gbvn], point [50.361328125, -4.04296875], doc_count [1248]
  39. key [u1j0], point [50.712890625, 7.20703125], doc_count [1156]
  40. key [u0j2], point [45.087890625, 7.55859375], doc_count [1138]
  41. ...
  42. --------------------------------------------------