Aggregations Refactor: Refactor Geobounds Aggregation

This commit is contained in:
Colin Goodheart-Smithe 2015-11-23 11:25:37 +00:00
parent 113df32b3a
commit 94e867906c
3 changed files with 129 additions and 37 deletions

View File

@ -20,10 +20,14 @@
package org.elasticsearch.search.aggregations.metrics.geobounds;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.DoubleArray;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.InternalAggregation;
@ -32,16 +36,20 @@ import org.elasticsearch.search.aggregations.LeafBucketCollectorBase;
import org.elasticsearch.search.aggregations.metrics.MetricsAggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
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.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class GeoBoundsAggregator extends MetricsAggregator {
static final ParseField WRAP_LONGITUDE_FIELD = new ParseField("wrap_longitude");
private final ValuesSource.GeoPoint valuesSource;
private final boolean wrapLongitude;
DoubleArray tops;
@ -168,13 +176,26 @@ public final class GeoBoundsAggregator extends MetricsAggregator {
public static class Factory extends ValuesSourceAggregatorFactory<ValuesSource.GeoPoint> {
private final boolean wrapLongitude;
private boolean wrapLongitude = true;
protected Factory(String name, ValuesSourceParser.Input<ValuesSource.GeoPoint> input, boolean wrapLongitude) {
super(name, InternalGeoBounds.TYPE, input);
public Factory(String name) {
super(name, InternalGeoBounds.TYPE, ValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}
/**
* Set whether to wrap longitudes. Defaults to true.
*/
public void wrapLongitude(boolean wrapLongitude) {
this.wrapLongitude = wrapLongitude;
}
/**
* Get whether to wrap longitudes.
*/
public boolean wrapLongitude() {
return wrapLongitude;
}
@Override
protected Aggregator createUnmapped(AggregationContext aggregationContext, Aggregator parent,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
@ -188,5 +209,35 @@ public final class GeoBoundsAggregator extends MetricsAggregator {
return new GeoBoundsAggregator(name, aggregationContext, parent, valuesSource, wrapLongitude, pipelineAggregators, metaData);
}
@Override
protected ValuesSourceAggregatorFactory<ValuesSource.GeoPoint> innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {
Factory factory = new Factory(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(WRAP_LONGITUDE_FIELD.getPreferredName(), wrapLongitude);
return builder;
}
@Override
protected int innerHashCode() {
return Objects.hash(wrapLongitude);
}
@Override
protected boolean innerEquals(Object obj) {
Factory other = (Factory) obj;
return Objects.equals(wrapLongitude, other.wrapLongitude);
}
}
}

View File

@ -19,18 +19,25 @@
package org.elasticsearch.search.aggregations.metrics.geobounds;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.GeoPointValuesSourceParser;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource.GeoPoint;
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
import java.util.Map;
public class GeoBoundsParser implements Aggregator.Parser {
public class GeoBoundsParser extends GeoPointValuesSourceParser {
public GeoBoundsParser() {
super(false, false);
}
@Override
public String type() {
@ -38,39 +45,31 @@ public class GeoBoundsParser implements Aggregator.Parser {
}
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
ValuesSourceParser<GeoPoint> vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoBounds.TYPE, context)
.targetValueType(ValueType.GEOPOINT)
.formattable(true)
.build();
boolean wrapLongitude = true;
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (vsParser.token(currentFieldName, token, parser)) {
continue;
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if ("wrap_longitude".equals(currentFieldName) || "wrapLongitude".equals(currentFieldName)) {
wrapLongitude = parser.booleanValue();
} else {
throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
+ currentFieldName + "].", parser.getTokenLocation());
}
} else {
throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
+ currentFieldName + "].", parser.getTokenLocation());
}
protected ValuesSourceAggregatorFactory<GeoPoint> createFactory(String aggregationName, ValuesSourceType valuesSourceType,
ValueType targetValueType, Map<ParseField, Object> otherOptions) {
GeoBoundsAggregator.Factory factory = new GeoBoundsAggregator.Factory(aggregationName);
Boolean wrapLongitude = (Boolean) otherOptions.get(GeoBoundsAggregator.WRAP_LONGITUDE_FIELD);
if (wrapLongitude != null) {
factory.wrapLongitude(wrapLongitude);
}
return new GeoBoundsAggregator.Factory(aggregationName, vsParser.input(), wrapLongitude);
return factory;
}
@Override
protected boolean token(String aggregationName, String currentFieldName, Token token, XContentParser parser,
ParseFieldMatcher parseFieldMatcher, Map<ParseField, Object> otherOptions) throws IOException {
if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (parseFieldMatcher.match(currentFieldName, GeoBoundsAggregator.WRAP_LONGITUDE_FIELD)) {
otherOptions.put(GeoBoundsAggregator.WRAP_LONGITUDE_FIELD, parser.booleanValue());
return true;
}
}
return false;
}
// NORELEASE implement this method when refactoring this aggregation
@Override
public AggregatorFactory[] getFactoryPrototypes() {
return null;
return new AggregatorFactory[] { new GeoBoundsAggregator.Factory(null) };
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.metrics;
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregator;
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregator.Factory;
public class GeoBoundsTests extends BaseAggregationTestCase<GeoBoundsAggregator.Factory> {
@Override
protected Factory createTestAggregatorFactory() {
Factory factory = new Factory(randomAsciiOfLengthBetween(1, 20));
String field = randomAsciiOfLengthBetween(3, 20);
factory.field(field);
if (randomBoolean()) {
factory.wrapLongitude(randomBoolean());
}
if (randomBoolean()) {
factory.missing("0,0");
}
return factory;
}
}