Skip to content

Commit 2a99eaa

Browse files
committed
Revert "removes the CellIdSource abstraction from geo-grid aggs (#45307) (#45353)"
This reverts commit 7b0a804.
1 parent 1794718 commit 2a99eaa

File tree

6 files changed

+123
-37
lines changed

6 files changed

+123
-37
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.search.aggregations.bucket.geogrid;
20+
21+
import org.apache.lucene.index.LeafReaderContext;
22+
import org.apache.lucene.index.SortedNumericDocValues;
23+
import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues;
24+
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
25+
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
26+
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
27+
import org.elasticsearch.search.aggregations.support.ValuesSource;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* Wrapper class to help convert {@link MultiGeoPointValues}
33+
* to numeric long values for bucketing.
34+
*/
35+
class CellIdSource extends ValuesSource.Numeric {
36+
private final ValuesSource.GeoPoint valuesSource;
37+
private final int precision;
38+
private final GeoPointLongEncoder encoder;
39+
40+
CellIdSource(GeoPoint valuesSource, int precision, GeoPointLongEncoder encoder) {
41+
this.valuesSource = valuesSource;
42+
//different GeoPoints could map to the same or different hashing cells.
43+
this.precision = precision;
44+
this.encoder = encoder;
45+
}
46+
47+
public int precision() {
48+
return precision;
49+
}
50+
51+
@Override
52+
public boolean isFloatingPoint() {
53+
return false;
54+
}
55+
56+
@Override
57+
public SortedNumericDocValues longValues(LeafReaderContext ctx) {
58+
return new CellValues(valuesSource.geoPointValues(ctx), precision, encoder);
59+
}
60+
61+
@Override
62+
public SortedNumericDoubleValues doubleValues(LeafReaderContext ctx) {
63+
throw new UnsupportedOperationException();
64+
}
65+
66+
@Override
67+
public SortedBinaryDocValues bytesValues(LeafReaderContext ctx) {
68+
throw new UnsupportedOperationException();
69+
}
70+
71+
/**
72+
* The encoder to use to convert a geopoint's (lon, lat, precision) into
73+
* a long-encoded bucket key for aggregating.
74+
*/
75+
@FunctionalInterface
76+
public interface GeoPointLongEncoder {
77+
long encode(double lon, double lat, int precision);
78+
}
79+
80+
private static class CellValues extends AbstractSortingNumericDocValues {
81+
private MultiGeoPointValues geoValues;
82+
private int precision;
83+
private GeoPointLongEncoder encoder;
84+
85+
protected CellValues(MultiGeoPointValues geoValues, int precision, GeoPointLongEncoder encoder) {
86+
this.geoValues = geoValues;
87+
this.precision = precision;
88+
this.encoder = encoder;
89+
}
90+
91+
@Override
92+
public boolean advanceExact(int docId) throws IOException {
93+
if (geoValues.advanceExact(docId)) {
94+
resize(geoValues.docValueCount());
95+
for (int i = 0; i < docValueCount(); ++i) {
96+
org.elasticsearch.common.geo.GeoPoint target = geoValues.nextValue();
97+
values[i] = encoder.encode(target.getLon(), target.getLat(), precision);
98+
}
99+
sort();
100+
return true;
101+
} else {
102+
return false;
103+
}
104+
}
105+
}
106+
}

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

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

2121
import org.apache.lucene.index.LeafReaderContext;
22+
import org.apache.lucene.index.SortedNumericDocValues;
2223
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;
2726
import org.elasticsearch.search.aggregations.Aggregator;
2827
import org.elasticsearch.search.aggregations.AggregatorFactories;
2928
import org.elasticsearch.search.aggregations.InternalAggregations;
3029
import org.elasticsearch.search.aggregations.LeafBucketCollector;
3130
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase;
3231
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator;
3332
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
34-
import org.elasticsearch.search.aggregations.support.ValuesSource;
3533
import org.elasticsearch.search.internal.SearchContext;
3634

3735
import java.io.IOException;
@@ -47,19 +45,14 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
4745

4846
protected final int requiredSize;
4947
protected final int shardSize;
50-
protected final ValuesSource.GeoPoint valuesSource;
51-
protected final int precision;
52-
protected final GeoPointLongEncoder longEncoder;
48+
protected final CellIdSource valuesSource;
5349
protected final LongHash bucketOrds;
5450

55-
GeoGridAggregator(String name, AggregatorFactories factories, ValuesSource.GeoPoint valuesSource,
56-
int precision, GeoPointLongEncoder longEncoder,
51+
GeoGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
5752
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
5853
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
5954
super(name, factories, aggregationContext, parent, pipelineAggregators, metaData);
6055
this.valuesSource = valuesSource;
61-
this.precision = precision;
62-
this.longEncoder = longEncoder;
6356
this.requiredSize = requiredSize;
6457
this.shardSize = shardSize;
6558
bucketOrds = new LongHash(1, aggregationContext.bigArrays());
@@ -76,7 +69,7 @@ public ScoreMode scoreMode() {
7669
@Override
7770
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
7871
final LeafBucketCollector sub) throws IOException {
79-
final MultiGeoPointValues values = valuesSource.geoPointValues(ctx);
72+
final SortedNumericDocValues values = valuesSource.longValues(ctx);
8073
return new LeafBucketCollectorBase(sub, null) {
8174
@Override
8275
public void collect(int doc, long bucket) throws IOException {
@@ -86,8 +79,7 @@ public void collect(int doc, long bucket) throws IOException {
8679

8780
long previous = Long.MAX_VALUE;
8881
for (int i = 0; i < valuesCount; ++i) {
89-
final GeoPoint point = values.nextValue();
90-
final long val = longEncoder.encode(point.getLon(), point.getLat(), precision);
82+
final long val = values.nextValue();
9183
if (previous != val || i == 0) {
9284
long bucketOrdinal = bucketOrds.add(val);
9385
if (bucketOrdinal < 0) { // already seen
@@ -197,12 +189,4 @@ public void doClose() {
197189
Releasables.close(bucketOrds);
198190
}
199191

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-
}
208192
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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;
2524
import org.elasticsearch.search.internal.SearchContext;
2625

2726
import java.io.IOException;
@@ -34,12 +33,10 @@
3433
*/
3534
public class GeoHashGridAggregator extends GeoGridAggregator<InternalGeoHashGrid> {
3635

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);
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);
4340
}
4441

4542
@Override

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

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

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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;
2625
import org.elasticsearch.search.internal.SearchContext;
2726

2827
import java.io.IOException;
@@ -35,12 +34,10 @@
3534
*/
3635
public class GeoTileGridAggregator extends GeoGridAggregator<InternalGeoTileGrid> {
3736

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

4643
@Override

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

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

0 commit comments

Comments
 (0)