Cut geo aggregations to registerAggregation

and remove their prototypes.

Relates to #17085
This commit is contained in:
Nik Everett 2016-04-15 11:08:32 -04:00
parent b55368b39d
commit c94302d246
9 changed files with 144 additions and 141 deletions

View File

@ -107,6 +107,7 @@ import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregatorBuild
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridParser;
import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid;
import org.elasticsearch.search.aggregations.bucket.global.GlobalParser;
@ -128,6 +129,7 @@ import org.elasticsearch.search.aggregations.bucket.range.RangeParser;
import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeParser;
import org.elasticsearch.search.aggregations.bucket.range.date.InternalDateRange;
import org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanceAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanceParser;
import org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance;
import org.elasticsearch.search.aggregations.bucket.range.ipv4.IPv4RangeAggregatorBuilder;
@ -155,8 +157,10 @@ import org.elasticsearch.search.aggregations.metrics.avg.AvgParser;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityParser;
import org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality;
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregatorBuilder;
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsParser;
import org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds;
import org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroidAggregatorBuilder;
import org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroidParser;
import org.elasticsearch.search.aggregations.metrics.geocentroid.InternalGeoCentroid;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
@ -472,14 +476,16 @@ public class SearchModule extends AbstractModule {
registerAggregation(HistogramAggregatorBuilder::new, new HistogramParser(), HistogramAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(DateHistogramAggregatorBuilder::new, new DateHistogramParser(),
DateHistogramAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new GeoDistanceParser());
registerAggregatorParser(new GeoHashGridParser());
registerAggregation(GeoDistanceAggregatorBuilder::new, new GeoDistanceParser(),
GeoDistanceAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(GeoGridAggregatorBuilder::new, new GeoHashGridParser(), GeoGridAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(NestedAggregatorBuilder::new, NestedAggregatorBuilder::parse, NestedAggregatorBuilder.AGGREGATION_FIELD_NAME);
registerAggregation(ReverseNestedAggregatorBuilder::new, ReverseNestedAggregatorBuilder::parse,
ReverseNestedAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new TopHitsParser());
registerAggregatorParser(new GeoBoundsParser());
registerAggregatorParser(new GeoCentroidParser());
registerAggregation(GeoBoundsAggregatorBuilder::new, new GeoBoundsParser(), GeoBoundsAggregatorBuilder.AGGREGATION_NAME_FIED);
registerAggregation(GeoCentroidAggregatorBuilder::new, new GeoCentroidParser(),
GeoCentroidAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(ScriptedMetricAggregatorBuilder::new, ScriptedMetricAggregatorBuilder::parse,
ScriptedMetricAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(ChildrenAggregatorBuilder::new, ChildrenAggregatorBuilder::parse,

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.bucket.geogrid;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.stream.StreamInput;
@ -45,8 +46,8 @@ import java.io.IOException;
import java.util.Objects;
public class GeoGridAggregatorBuilder extends ValuesSourceAggregatorBuilder<ValuesSource.GeoPoint, GeoGridAggregatorBuilder> {
static final GeoGridAggregatorBuilder PROTOTYPE = new GeoGridAggregatorBuilder("");
public static final String NAME = InternalGeoHashGrid.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
private int precision = GeoHashGridParser.DEFAULT_PRECISION;
private int requiredSize = GeoHashGridParser.DEFAULT_MAX_NUM_CELLS;
@ -56,6 +57,28 @@ public class GeoGridAggregatorBuilder extends ValuesSourceAggregatorBuilder<Valu
super(name, InternalGeoHashGrid.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}
/**
* Read from a stream.
*/
public GeoGridAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalGeoHashGrid.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
precision = in.readVInt();
requiredSize = in.readVInt();
shardSize = in.readVInt();
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeVInt(precision);
out.writeVInt(requiredSize);
out.writeVInt(shardSize);
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
public GeoGridAggregatorBuilder precision(int precision) {
this.precision = GeoHashGridParams.checkPrecision(precision);
return this;
@ -96,44 +119,27 @@ public class GeoGridAggregatorBuilder extends ValuesSourceAggregatorBuilder<Valu
ValuesSourceConfig<ValuesSource.GeoPoint> config, AggregatorFactory<?> parent, Builder subFactoriesBuilder)
throws IOException {
int shardSize = this.shardSize;
if (shardSize == 0) {
shardSize = Integer.MAX_VALUE;
}
if (shardSize == 0) {
shardSize = Integer.MAX_VALUE;
}
int requiredSize = this.requiredSize;
if (requiredSize == 0) {
requiredSize = Integer.MAX_VALUE;
}
if (requiredSize == 0) {
requiredSize = Integer.MAX_VALUE;
}
if (shardSize < 0) {
if (shardSize < 0) {
// Use default heuristic to avoid any wrong-ranking caused by distributed counting
shardSize = BucketUtils.suggestShardSideQueueSize(requiredSize, context.searchContext().numberOfShards());
}
}
if (shardSize < requiredSize) {
shardSize = requiredSize;
}
if (shardSize < requiredSize) {
shardSize = requiredSize;
}
return new GeoHashGridAggregatorFactory(name, type, config, precision, requiredSize, shardSize, context, parent,
subFactoriesBuilder, metaData);
}
@Override
protected GeoGridAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType, ValueType targetValueType,
StreamInput in) throws IOException {
GeoGridAggregatorBuilder factory = new GeoGridAggregatorBuilder(name);
factory.precision = in.readVInt();
factory.requiredSize = in.readVInt();
factory.shardSize = in.readVInt();
return factory;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeVInt(precision);
out.writeVInt(requiredSize);
out.writeVInt(shardSize);
}
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(GeoHashGridParams.FIELD_PRECISION.getPreferredName(), precision);
@ -162,6 +168,11 @@ public class GeoGridAggregatorBuilder extends ValuesSourceAggregatorBuilder<Valu
return Objects.hash(precision, requiredSize, shardSize);
}
@Override
public String getWriteableName() {
return NAME;
}
private static class CellValues extends SortingNumericDocValues {
private MultiGeoPointValues geoValues;
private int precision;

View File

@ -44,15 +44,6 @@ public class GeoHashGridParser extends GeoPointValuesSourceParser {
super(false, false);
}
@Override
public String type() {
return InternalGeoHashGrid.TYPE.name();
}
@Override
public GeoGridAggregatorBuilder getFactoryPrototypes() {
return GeoGridAggregatorBuilder.PROTOTYPE;
}
@Override
protected GeoGridAggregatorBuilder createFactory(
String aggregationName, ValuesSourceType valuesSourceType,

View File

@ -19,24 +19,23 @@
package org.elasticsearch.search.aggregations.bucket.range.geodistance;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.bucket.range.InternalRange;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator;
import org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanceParser.Range;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
import java.util.ArrayList;
@ -44,8 +43,8 @@ import java.util.List;
import java.util.Objects;
public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder<ValuesSource.GeoPoint, GeoDistanceAggregatorBuilder> {
static final GeoDistanceAggregatorBuilder PROTOTYPE = new GeoDistanceAggregatorBuilder("", new GeoPoint());
public static final String NAME = InternalGeoDistance.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
private final GeoPoint origin;
private List<Range> ranges = new ArrayList<>();
@ -66,6 +65,41 @@ public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder<
this.origin = origin;
}
/**
* Read from a stream.
*/
public GeoDistanceAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalGeoDistance.FACTORY.type(), InternalGeoDistance.FACTORY.getValueSourceType(),
InternalGeoDistance.FACTORY.getValueType());
origin = new GeoPoint(in.readDouble(), in.readDouble());
int size = in.readVInt();
ranges = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
ranges.add(Range.PROTOTYPE.readFrom(in));
}
keyed = in.readBoolean();
distanceType = GeoDistance.readFromStream(in);
unit = DistanceUnit.readFromStream(in);
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeDouble(origin.lat());
out.writeDouble(origin.lon());
out.writeVInt(ranges.size());
for (Range range : ranges) {
range.writeTo(out);
}
out.writeBoolean(keyed);
distanceType.writeTo(out);
unit.writeTo(out);
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
public GeoDistanceAggregatorBuilder addRange(Range range) {
if (range == null) {
throw new IllegalArgumentException("[range] must not be null: [" + name + "]");
@ -146,7 +180,7 @@ public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder<
@Override
public String getWriteableName() {
return InternalGeoDistance.TYPE.name();
return NAME;
}
public GeoDistanceAggregatorBuilder unit(DistanceUnit unit) {
@ -200,34 +234,6 @@ public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder<
return builder;
}
@Override
protected GeoDistanceAggregatorBuilder innerReadFrom(
String name, ValuesSourceType valuesSourceType, ValueType targetValueType, StreamInput in) throws IOException {
GeoPoint origin = new GeoPoint(in.readDouble(), in.readDouble());
int size = in.readVInt();
GeoDistanceAggregatorBuilder factory = new GeoDistanceAggregatorBuilder(name, origin);
for (int i = 0; i < size; i++) {
factory.addRange(Range.PROTOTYPE.readFrom(in));
}
factory.keyed = in.readBoolean();
factory.distanceType = GeoDistance.readFromStream(in);
factory.unit = DistanceUnit.readFromStream(in);
return factory;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeDouble(origin.lat());
out.writeDouble(origin.lon());
out.writeVInt(ranges.size());
for (Range range : ranges) {
range.writeTo(out);
}
out.writeBoolean(keyed);
distanceType.writeTo(out);
unit.writeTo(out);
}
@Override
protected int innerHashCode() {
return Objects.hash(origin, ranges, keyed, distanceType, unit);

View File

@ -53,11 +53,6 @@ public class GeoDistanceParser extends GeoPointValuesSourceParser {
super(true, false);
}
@Override
public String type() {
return InternalGeoDistance.TYPE.name();
}
public static class Range extends RangeAggregator.Range {
static final Range PROTOTYPE = new Range(null, null, null);
@ -181,10 +176,4 @@ public class GeoDistanceParser extends GeoPointValuesSourceParser {
}
return false;
}
@Override
public GeoDistanceAggregatorBuilder getFactoryPrototypes() {
return GeoDistanceAggregatorBuilder.PROTOTYPE;
}
}

View File

@ -19,23 +19,25 @@
package org.elasticsearch.search.aggregations.metrics.geobounds;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
import java.util.Objects;
public class GeoBoundsAggregatorBuilder extends ValuesSourceAggregatorBuilder<ValuesSource.GeoPoint, GeoBoundsAggregatorBuilder> {
static final GeoBoundsAggregatorBuilder PROTOTYPE = new GeoBoundsAggregatorBuilder("");
public static final String NAME = InternalGeoBounds.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIED = new ParseField(NAME);
private boolean wrapLongitude = true;
@ -43,6 +45,24 @@ public class GeoBoundsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Va
super(name, InternalGeoBounds.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}
/**
* Read from a stream.
*/
public GeoBoundsAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalGeoBounds.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
wrapLongitude = in.readBoolean();
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeBoolean(wrapLongitude);
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
/**
* Set whether to wrap longitudes. Defaults to true.
*/
@ -64,19 +84,6 @@ public class GeoBoundsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Va
return new GeoBoundsAggregatorFactory(name, type, config, wrapLongitude, context, parent, subFactoriesBuilder, metaData);
}
@Override
protected GeoBoundsAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {
GeoBoundsAggregatorBuilder factory = new GeoBoundsAggregatorBuilder(name);
factory.wrapLongitude = in.readBoolean();
return factory;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeBoolean(wrapLongitude);
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(GeoBoundsAggregator.WRAP_LONGITUDE_FIELD.getPreferredName(), wrapLongitude);
@ -94,4 +101,8 @@ public class GeoBoundsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Va
return Objects.equals(wrapLongitude, other.wrapLongitude);
}
@Override
public String getWriteableName() {
return NAME;
}
}

View File

@ -36,11 +36,6 @@ public class GeoBoundsParser extends GeoPointValuesSourceParser {
super(false, false);
}
@Override
public String type() {
return InternalGeoBounds.TYPE.name();
}
@Override
protected GeoBoundsAggregatorBuilder createFactory(String aggregationName, ValuesSourceType valuesSourceType,
ValueType targetValueType, Map<ParseField, Object> otherOptions) {
@ -63,10 +58,4 @@ public class GeoBoundsParser extends GeoPointValuesSourceParser {
}
return false;
}
@Override
public GeoBoundsAggregatorBuilder getFactoryPrototypes() {
return GeoBoundsAggregatorBuilder.PROTOTYPE;
}
}

View File

@ -19,48 +19,53 @@
package org.elasticsearch.search.aggregations.metrics.geocentroid;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSource.GeoPoint;
import java.io.IOException;
public class GeoCentroidAggregatorBuilder
extends ValuesSourceAggregatorBuilder.LeafOnly<ValuesSource.GeoPoint, GeoCentroidAggregatorBuilder> {
static final GeoCentroidAggregatorBuilder PROTOTYPE = new GeoCentroidAggregatorBuilder("");
public static final String NAME = InternalGeoCentroid.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
public GeoCentroidAggregatorBuilder(String name) {
super(name, InternalGeoCentroid.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}
/**
* Read from a stream.
*/
public GeoCentroidAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalGeoCentroid.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}
@Override
protected void innerWriteTo(StreamOutput out) {
// Do nothing, no extra state to write to stream
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
@Override
protected GeoCentroidAggregatorFactory innerBuild(AggregationContext context, ValuesSourceConfig<ValuesSource.GeoPoint> config,
AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException {
return new GeoCentroidAggregatorFactory(name, type, config, context, parent, subFactoriesBuilder, metaData);
}
@Override
protected GeoCentroidAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {
return new GeoCentroidAggregatorBuilder(name);
}
@Override
protected void innerWriteTo(StreamOutput out) {
// Do nothing, no extra state to write to stream
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
return builder;
@ -75,4 +80,9 @@ public class GeoCentroidAggregatorBuilder
protected boolean innerEquals(Object obj) {
return true;
}
@Override
public String getWriteableName() {
return NAME;
}
}

View File

@ -39,11 +39,6 @@ public class GeoCentroidParser extends GeoPointValuesSourceParser {
super(true, false);
}
@Override
public String type() {
return InternalGeoCentroid.TYPE.name();
}
@Override
protected boolean token(String aggregationName, String currentFieldName, Token token, XContentParser parser,
ParseFieldMatcher parseFieldMatcher, Map<ParseField, Object> otherOptions) throws IOException {
@ -55,9 +50,4 @@ public class GeoCentroidParser extends GeoPointValuesSourceParser {
ValueType targetValueType, Map<ParseField, Object> otherOptions) {
return new GeoCentroidAggregatorBuilder(aggregationName);
}
@Override
public GeoCentroidAggregatorBuilder getFactoryPrototypes() {
return GeoCentroidAggregatorBuilder.PROTOTYPE;
}
}