2014-05-15 06:36:05 -04:00
|
|
|
[[java-aggs-bucket-geodistance]]
|
|
|
|
==== Geo Distance Aggregation
|
|
|
|
|
|
|
|
Here is how you can use
|
|
|
|
{ref}/search-aggregations-bucket-geodistance-aggregation.html[Geo Distance Aggregation]
|
|
|
|
with Java API.
|
|
|
|
|
|
|
|
|
|
|
|
===== Prepare aggregation request
|
|
|
|
|
|
|
|
Here is an example on how to create the aggregation request:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilder aggregation =
|
|
|
|
AggregationBuilders
|
|
|
|
.geoDistance("agg")
|
|
|
|
.field("address.location")
|
|
|
|
.point(new GeoPoint(48.84237171118314,2.33320027692004))
|
|
|
|
.unit(DistanceUnit.KILOMETERS)
|
|
|
|
.addUnboundedTo(3.0)
|
|
|
|
.addRange(3.0, 10.0)
|
|
|
|
.addRange(10.0, 500.0);
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
===== Use aggregation response
|
|
|
|
|
|
|
|
Import Aggregation definition classes:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2015-03-05 05:56:13 -05:00
|
|
|
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
2014-05-15 06:36:05 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// sr is here your SearchResponse object
|
2015-03-05 05:56:13 -05:00
|
|
|
Range agg = sr.getAggregations().get("agg");
|
2014-05-15 06:36:05 -04:00
|
|
|
|
|
|
|
// For each entry
|
2015-03-05 05:56:13 -05:00
|
|
|
for (Range.Bucket entry : agg.getBuckets()) {
|
2015-06-03 11:08:32 -04:00
|
|
|
String key = entry.getKeyAsString(); // key as String
|
|
|
|
Number from = (Number) entry.getFrom(); // bucket from value
|
|
|
|
Number to = (Number) entry.getTo(); // bucket to value
|
|
|
|
long docCount = entry.getDocCount(); // Doc count
|
2014-05-15 06:36:05 -04:00
|
|
|
|
|
|
|
logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, from, to, docCount);
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
This will basically produce:
|
|
|
|
|
|
|
|
[source,text]
|
|
|
|
--------------------------------------------------
|
|
|
|
key [*-3.0], from [0.0], to [3.0], doc_count [161]
|
|
|
|
key [3.0-10.0], from [3.0], to [10.0], doc_count [460]
|
|
|
|
key [10.0-500.0], from [10.0], to [500.0], doc_count [4925]
|
|
|
|
--------------------------------------------------
|
|
|
|
|