Skip to content

Commit f114ef6

Browse files
authored
removes the CellIdSource abstraction from geo-grid aggs (#45307)
CellIdSource is a helper ValuesSource that encodes GeoPoint into a long-encoded representation of the grid bucket the point is associated with. This complicates thing as usage evolves to support shapes that are associated with more than one bucket ordinal.
1 parent eb22677 commit f114ef6

File tree

6 files changed

+37
-123
lines changed

6 files changed

+37
-123
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/CellIdSource.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregator.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
package org.elasticsearch.search.aggregations.bucket.geogrid;
2020

2121
import org.apache.lucene.index.LeafReaderContext;
22-
import org.apache.lucene.index.SortedNumericDocValues;
2322
import org.apache.lucene.search.ScoreMode;
23+
import org.elasticsearch.common.geo.GeoPoint;
2424
import org.elasticsearch.common.lease.Releasables;
2525
import org.elasticsearch.common.util.LongHash;
26+
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
2627
import org.elasticsearch.search.aggregations.Aggregator;
2728
import org.elasticsearch.search.aggregations.AggregatorFactories;
2829
import org.elasticsearch.search.aggregations.InternalAggregations;
2930
import org.elasticsearch.search.aggregations.LeafBucketCollector;
3031
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase;
3132
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator;
3233
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
34+
import org.elasticsearch.search.aggregations.support.ValuesSource;
3335
import org.elasticsearch.search.internal.SearchContext;
3436

3537
import 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
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridAggregator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.search.aggregations.Aggregator;
2222
import org.elasticsearch.search.aggregations.AggregatorFactories;
2323
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
24+
import org.elasticsearch.search.aggregations.support.ValuesSource;
2425
import org.elasticsearch.search.internal.SearchContext;
2526

2627
import java.io.IOException;
@@ -33,10 +34,12 @@
3334
*/
3435
public class GeoHashGridAggregator extends GeoGridAggregator<InternalGeoHashGrid> {
3536

36-
GeoHashGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
37-
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
38-
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
39-
super(name, factories, valuesSource, requiredSize, shardSize, aggregationContext, parent, pipelineAggregators, metaData);
37+
GeoHashGridAggregator(String name, AggregatorFactories factories,
38+
ValuesSource.GeoPoint valuesSource, int precision, GeoPointLongEncoder longEncoder,
39+
SearchContext aggregationContext, Aggregator parent, List<PipelineAggregator> pipelineAggregators,
40+
Map<String, Object> metaData, int requiredSize, int shardSize) throws IOException {
41+
super(name, factories, valuesSource, precision, longEncoder, requiredSize, shardSize, aggregationContext, parent,
42+
pipelineAggregators, metaData);
4043
}
4144

4245
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridAggregatorFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ protected Aggregator doCreateInternal(final ValuesSource.GeoPoint valuesSource,
7171
if (collectsFromSingleBucket == false) {
7272
return asMultiBucketAggregator(this, context, parent);
7373
}
74-
CellIdSource cellIdSource = new CellIdSource(valuesSource, precision, Geohash::longEncode);
75-
return new GeoHashGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, context, parent,
76-
pipelineAggregators, metaData);
74+
return new GeoHashGridAggregator(name, factories, valuesSource, precision, Geohash::longEncode, context, parent,
75+
pipelineAggregators, metaData, requiredSize, shardSize);
7776
}
7877
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridAggregator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.search.aggregations.Aggregator;
2323
import org.elasticsearch.search.aggregations.AggregatorFactories;
2424
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
25+
import org.elasticsearch.search.aggregations.support.ValuesSource;
2526
import org.elasticsearch.search.internal.SearchContext;
2627

2728
import java.io.IOException;
@@ -34,10 +35,12 @@
3435
*/
3536
public class GeoTileGridAggregator extends GeoGridAggregator<InternalGeoTileGrid> {
3637

37-
GeoTileGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
38+
GeoTileGridAggregator(String name, AggregatorFactories factories, ValuesSource.GeoPoint valuesSource,
39+
int precision, GeoPointLongEncoder longEncoder,
3840
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
3941
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
40-
super(name, factories, valuesSource, requiredSize, shardSize, aggregationContext, parent, pipelineAggregators, metaData);
42+
super(name, factories, valuesSource, precision, longEncoder, requiredSize, shardSize, aggregationContext, parent,
43+
pipelineAggregators, metaData);
4144
}
4245

4346
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridAggregatorFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ protected Aggregator doCreateInternal(final ValuesSource.GeoPoint valuesSource,
7171
if (collectsFromSingleBucket == false) {
7272
return asMultiBucketAggregator(this, context, parent);
7373
}
74-
CellIdSource cellIdSource = new CellIdSource(valuesSource, precision, GeoTileUtils::longEncode);
75-
return new GeoTileGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, context, parent,
76-
pipelineAggregators, metaData);
74+
return new GeoTileGridAggregator(name, factories, valuesSource, precision, GeoTileUtils::longEncode, requiredSize,
75+
shardSize, context, parent, pipelineAggregators, metaData);
7776
}
7877
}

0 commit comments

Comments
 (0)