1919package org .elasticsearch .search .aggregations .bucket .geogrid ;
2020
2121import org .apache .lucene .index .LeafReaderContext ;
22- import org .apache .lucene .index .SortedNumericDocValues ;
2322import org .apache .lucene .search .ScoreMode ;
23+ import org .elasticsearch .common .geo .GeoPoint ;
2424import org .elasticsearch .common .lease .Releasables ;
2525import org .elasticsearch .common .util .LongHash ;
26+ import org .elasticsearch .index .fielddata .MultiGeoPointValues ;
2627import org .elasticsearch .search .aggregations .Aggregator ;
2728import org .elasticsearch .search .aggregations .AggregatorFactories ;
2829import org .elasticsearch .search .aggregations .InternalAggregations ;
2930import org .elasticsearch .search .aggregations .LeafBucketCollector ;
3031import org .elasticsearch .search .aggregations .LeafBucketCollectorBase ;
3132import org .elasticsearch .search .aggregations .bucket .BucketsAggregator ;
3233import org .elasticsearch .search .aggregations .pipeline .PipelineAggregator ;
34+ import org .elasticsearch .search .aggregations .support .ValuesSource ;
3335import org .elasticsearch .search .internal .SearchContext ;
3436
3537import java .io .IOException ;
@@ -45,14 +47,19 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
4547
4648 protected final int requiredSize ;
4749 protected final int shardSize ;
48- protected final CellIdSource valuesSource ;
50+ protected final ValuesSource .GeoPoint valuesSource ;
51+ protected final int precision ;
52+ protected final GeoPointLongEncoder longEncoder ;
4953 protected final LongHash bucketOrds ;
5054
51- GeoGridAggregator (String name , AggregatorFactories factories , CellIdSource valuesSource ,
55+ GeoGridAggregator (String name , AggregatorFactories factories , ValuesSource .GeoPoint valuesSource ,
56+ int precision , GeoPointLongEncoder longEncoder ,
5257 int requiredSize , int shardSize , SearchContext aggregationContext , Aggregator parent ,
5358 List <PipelineAggregator > pipelineAggregators , Map <String , Object > metaData ) throws IOException {
5459 super (name , factories , aggregationContext , parent , pipelineAggregators , metaData );
5560 this .valuesSource = valuesSource ;
61+ this .precision = precision ;
62+ this .longEncoder = longEncoder ;
5663 this .requiredSize = requiredSize ;
5764 this .shardSize = shardSize ;
5865 bucketOrds = new LongHash (1 , aggregationContext .bigArrays ());
@@ -69,7 +76,7 @@ public ScoreMode scoreMode() {
6976 @ Override
7077 public LeafBucketCollector getLeafCollector (LeafReaderContext ctx ,
7178 final LeafBucketCollector sub ) throws IOException {
72- final SortedNumericDocValues values = valuesSource .longValues (ctx );
79+ final MultiGeoPointValues values = valuesSource .geoPointValues (ctx );
7380 return new LeafBucketCollectorBase (sub , null ) {
7481 @ Override
7582 public void collect (int doc , long bucket ) throws IOException {
@@ -79,7 +86,8 @@ public void collect(int doc, long bucket) throws IOException {
7986
8087 long previous = Long .MAX_VALUE ;
8188 for (int i = 0 ; i < valuesCount ; ++i ) {
82- final long val = values .nextValue ();
89+ final GeoPoint point = values .nextValue ();
90+ final long val = longEncoder .encode (point .getLon (), point .getLat (), precision );
8391 if (previous != val || i == 0 ) {
8492 long bucketOrdinal = bucketOrds .add (val );
8593 if (bucketOrdinal < 0 ) { // already seen
@@ -189,4 +197,12 @@ public void doClose() {
189197 Releasables .close (bucketOrds );
190198 }
191199
200+ /**
201+ * The encoder to use to convert a geopoint's (lon, lat, precision) into
202+ * a long-encoded bucket key for aggregating.
203+ */
204+ @ FunctionalInterface
205+ public interface GeoPointLongEncoder {
206+ long encode (double lon , double lat , int precision );
207+ }
192208}
0 commit comments