2014-05-15 06:36:05 -04:00
|
|
|
[[java-aggs-bucket-geohashgrid]]
|
|
|
|
==== Geo Hash Grid Aggregation
|
|
|
|
|
|
|
|
Here is how you can use
|
|
|
|
{ref}/search-aggregations-bucket-geohashgrid-aggregation.html[Geo Hash Grid Aggregation]
|
|
|
|
with Java API.
|
|
|
|
|
|
|
|
|
|
|
|
===== Prepare aggregation request
|
|
|
|
|
|
|
|
Here is an example on how to create the aggregation request:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilder aggregation =
|
|
|
|
AggregationBuilders
|
|
|
|
.geohashGrid("agg")
|
|
|
|
.field("address.location")
|
|
|
|
.precision(4);
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
===== Use aggregation response
|
|
|
|
|
|
|
|
Import Aggregation definition classes:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// sr is here your SearchResponse object
|
|
|
|
GeoHashGrid agg = sr.getAggregations().get("agg");
|
|
|
|
|
|
|
|
// For each entry
|
|
|
|
for (GeoHashGrid.Bucket entry : agg.getBuckets()) {
|
2015-06-03 11:08:32 -04:00
|
|
|
String keyAsString = entry.getKeyAsString(); // key as String
|
|
|
|
GeoPoint key = (GeoPoint) entry.getKey(); // key as geo point
|
|
|
|
long docCount = entry.getDocCount(); // Doc count
|
2014-05-15 06:36:05 -04:00
|
|
|
|
2015-06-03 11:08:32 -04:00
|
|
|
logger.info("key [{}], point {}, doc_count [{}]", keyAsString, key, docCount);
|
2014-05-15 06:36:05 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
This will basically produce:
|
|
|
|
|
|
|
|
[source,text]
|
|
|
|
--------------------------------------------------
|
|
|
|
key [gbqu], point [47.197265625, -1.58203125], doc_count [1282]
|
|
|
|
key [gbvn], point [50.361328125, -4.04296875], doc_count [1248]
|
|
|
|
key [u1j0], point [50.712890625, 7.20703125], doc_count [1156]
|
|
|
|
key [u0j2], point [45.087890625, 7.55859375], doc_count [1138]
|
|
|
|
...
|
|
|
|
--------------------------------------------------
|
|
|
|
|