Revert "removes the CellIdSource abstraction from geo-grid aggs (#45307) (#45353)"

This reverts commit 7b0a8040de.
This commit is contained in:
Tal Levy 2019-08-08 17:40:03 -07:00
parent 1794718e8e
commit 2a99eaa7c2
6 changed files with 123 additions and 37 deletions

View File

@ -0,0 +1,106 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.bucket.geogrid;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import java.io.IOException;
/**
* Wrapper class to help convert {@link MultiGeoPointValues}
* to numeric long values for bucketing.
*/
class CellIdSource extends ValuesSource.Numeric {
private final ValuesSource.GeoPoint valuesSource;
private final int precision;
private final GeoPointLongEncoder encoder;
CellIdSource(GeoPoint valuesSource, int precision, GeoPointLongEncoder encoder) {
this.valuesSource = valuesSource;
//different GeoPoints could map to the same or different hashing cells.
this.precision = precision;
this.encoder = encoder;
}
public int precision() {
return precision;
}
@Override
public boolean isFloatingPoint() {
return false;
}
@Override
public SortedNumericDocValues longValues(LeafReaderContext ctx) {
return new CellValues(valuesSource.geoPointValues(ctx), precision, encoder);
}
@Override
public SortedNumericDoubleValues doubleValues(LeafReaderContext ctx) {
throw new UnsupportedOperationException();
}
@Override
public SortedBinaryDocValues bytesValues(LeafReaderContext ctx) {
throw new UnsupportedOperationException();
}
/**
* The encoder to use to convert a geopoint's (lon, lat, precision) into
* a long-encoded bucket key for aggregating.
*/
@FunctionalInterface
public interface GeoPointLongEncoder {
long encode(double lon, double lat, int precision);
}
private static class CellValues extends AbstractSortingNumericDocValues {
private MultiGeoPointValues geoValues;
private int precision;
private GeoPointLongEncoder encoder;
protected CellValues(MultiGeoPointValues geoValues, int precision, GeoPointLongEncoder encoder) {
this.geoValues = geoValues;
this.precision = precision;
this.encoder = encoder;
}
@Override
public boolean advanceExact(int docId) throws IOException {
if (geoValues.advanceExact(docId)) {
resize(geoValues.docValueCount());
for (int i = 0; i < docValueCount(); ++i) {
org.elasticsearch.common.geo.GeoPoint target = geoValues.nextValue();
values[i] = encoder.encode(target.getLon(), target.getLat(), precision);
}
sort();
return true;
} else {
return false;
}
}
}
}

View File

@ -19,11 +19,10 @@
package org.elasticsearch.search.aggregations.bucket.geogrid;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.ScoreMode;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.util.LongHash;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.InternalAggregations;
@ -31,7 +30,6 @@ import org.elasticsearch.search.aggregations.LeafBucketCollector;
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase;
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
@ -47,19 +45,14 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
protected final int requiredSize;
protected final int shardSize;
protected final ValuesSource.GeoPoint valuesSource;
protected final int precision;
protected final GeoPointLongEncoder longEncoder;
protected final CellIdSource valuesSource;
protected final LongHash bucketOrds;
GeoGridAggregator(String name, AggregatorFactories factories, ValuesSource.GeoPoint valuesSource,
int precision, GeoPointLongEncoder longEncoder,
GeoGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
super(name, factories, aggregationContext, parent, pipelineAggregators, metaData);
this.valuesSource = valuesSource;
this.precision = precision;
this.longEncoder = longEncoder;
this.requiredSize = requiredSize;
this.shardSize = shardSize;
bucketOrds = new LongHash(1, aggregationContext.bigArrays());
@ -76,7 +69,7 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
final LeafBucketCollector sub) throws IOException {
final MultiGeoPointValues values = valuesSource.geoPointValues(ctx);
final SortedNumericDocValues values = valuesSource.longValues(ctx);
return new LeafBucketCollectorBase(sub, null) {
@Override
public void collect(int doc, long bucket) throws IOException {
@ -86,8 +79,7 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
long previous = Long.MAX_VALUE;
for (int i = 0; i < valuesCount; ++i) {
final GeoPoint point = values.nextValue();
final long val = longEncoder.encode(point.getLon(), point.getLat(), precision);
final long val = values.nextValue();
if (previous != val || i == 0) {
long bucketOrdinal = bucketOrds.add(val);
if (bucketOrdinal < 0) { // already seen
@ -197,12 +189,4 @@ public abstract class GeoGridAggregator<T extends InternalGeoGrid> extends Bucke
Releasables.close(bucketOrds);
}
/**
* The encoder to use to convert a geopoint's (lon, lat, precision) into
* a long-encoded bucket key for aggregating.
*/
@FunctionalInterface
public interface GeoPointLongEncoder {
long encode(double lon, double lat, int precision);
}
}

View File

@ -21,7 +21,6 @@ package org.elasticsearch.search.aggregations.bucket.geogrid;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
@ -34,12 +33,10 @@ import java.util.Map;
*/
public class GeoHashGridAggregator extends GeoGridAggregator<InternalGeoHashGrid> {
GeoHashGridAggregator(String name, AggregatorFactories factories,
ValuesSource.GeoPoint valuesSource, int precision, GeoPointLongEncoder longEncoder,
SearchContext aggregationContext, Aggregator parent, List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData, int requiredSize, int shardSize) throws IOException {
super(name, factories, valuesSource, precision, longEncoder, requiredSize, shardSize, aggregationContext, parent,
pipelineAggregators, metaData);
GeoHashGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
super(name, factories, valuesSource, requiredSize, shardSize, aggregationContext, parent, pipelineAggregators, metaData);
}
@Override

View File

@ -71,7 +71,8 @@ public class GeoHashGridAggregatorFactory extends ValuesSourceAggregatorFactory<
if (collectsFromSingleBucket == false) {
return asMultiBucketAggregator(this, context, parent);
}
return new GeoHashGridAggregator(name, factories, valuesSource, precision, Geohash::longEncode, context, parent,
pipelineAggregators, metaData, requiredSize, shardSize);
CellIdSource cellIdSource = new CellIdSource(valuesSource, precision, Geohash::longEncode);
return new GeoHashGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, context, parent,
pipelineAggregators, metaData);
}
}

View File

@ -22,7 +22,6 @@ package org.elasticsearch.search.aggregations.bucket.geogrid;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
@ -35,12 +34,10 @@ import java.util.Map;
*/
public class GeoTileGridAggregator extends GeoGridAggregator<InternalGeoTileGrid> {
GeoTileGridAggregator(String name, AggregatorFactories factories, ValuesSource.GeoPoint valuesSource,
int precision, GeoPointLongEncoder longEncoder,
GeoTileGridAggregator(String name, AggregatorFactories factories, CellIdSource valuesSource,
int requiredSize, int shardSize, SearchContext aggregationContext, Aggregator parent,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
super(name, factories, valuesSource, precision, longEncoder, requiredSize, shardSize, aggregationContext, parent,
pipelineAggregators, metaData);
super(name, factories, valuesSource, requiredSize, shardSize, aggregationContext, parent, pipelineAggregators, metaData);
}
@Override

View File

@ -71,7 +71,8 @@ public class GeoTileGridAggregatorFactory extends ValuesSourceAggregatorFactory<
if (collectsFromSingleBucket == false) {
return asMultiBucketAggregator(this, context, parent);
}
return new GeoTileGridAggregator(name, factories, valuesSource, precision, GeoTileUtils::longEncode, requiredSize,
shardSize, context, parent, pipelineAggregators, metaData);
CellIdSource cellIdSource = new CellIdSource(valuesSource, precision, GeoTileUtils::longEncode);
return new GeoTileGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, context, parent,
pipelineAggregators, metaData);
}
}