Cut terms aggregation to registerAggregation

and remove its PROTOTYPE. This is the first aggregation builder that
serializes its targetValueType so ValuesSourceAggregatorBuilder had to
grow support for that.

Relates to #17085
This commit is contained in:
Nik Everett 2016-04-14 15:34:11 -04:00
parent cf80b00507
commit e3c65408ec
6 changed files with 85 additions and 60 deletions

View File

@ -146,6 +146,7 @@ import org.elasticsearch.search.aggregations.bucket.significant.heuristics.Signi
import org.elasticsearch.search.aggregations.bucket.terms.DoubleTerms;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsParser;
import org.elasticsearch.search.aggregations.bucket.terms.UnmappedTerms;
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregatorBuilder;
@ -460,7 +461,7 @@ public class SearchModule extends AbstractModule {
FiltersAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new SamplerParser());
registerAggregatorParser(new DiversifiedSamplerParser());
registerAggregatorParser(new TermsParser());
registerAggregation(TermsAggregatorBuilder::new, new TermsParser(), TermsAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new SignificantTermsParser(significanceHeuristicParserMapper, queryParserRegistry));
registerAggregation(RangeAggregatorBuilder::new, new RangeParser(), RangeAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(DateRangeAggregatorBuilder::new, new DateRangeParser(), DateRangeAggregatorBuilder.AGGREGATION_NAME_FIELD);

View File

@ -20,7 +20,6 @@ package org.elasticsearch.search.aggregations.bucket.significant;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.index.query.QueryBuilder;
@ -47,7 +46,6 @@ public class SignificantTermsParser extends AbstractTermsParser {
private final SignificanceHeuristicParserMapper significanceHeuristicParserMapper;
private final IndicesQueriesRegistry queriesRegistry;
@Inject
public SignificantTermsParser(SignificanceHeuristicParserMapper significanceHeuristicParserMapper,
IndicesQueriesRegistry queriesRegistry) {
this.significanceHeuristicParserMapper = significanceHeuristicParserMapper;

View File

@ -38,13 +38,9 @@ import java.io.IOException;
import java.util.List;
import java.util.Objects;
/**
*
*/
/**
*
*/
public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder<ValuesSource, TermsAggregatorBuilder> {
public static final String NAME = StringTerms.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
public static final ParseField EXECUTION_HINT_FIELD_NAME = new ParseField("execution_hint");
public static final ParseField SHARD_SIZE_FIELD_NAME = new ParseField("shard_size");
@ -57,8 +53,6 @@ public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Values
public static final ParseField SHOW_TERM_DOC_COUNT_ERROR = new ParseField("show_term_doc_count_error");
public static final ParseField ORDER_FIELD = new ParseField("order");
static final TermsAggregatorBuilder PROTOTYPE = new TermsAggregatorBuilder("", null);
private Terms.Order order = Terms.Order.compound(Terms.Order.count(false), Terms.Order.term(true));
private IncludeExclude includeExclude = null;
private String executionHint = null;
@ -71,6 +65,45 @@ public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Values
super(name, StringTerms.TYPE, ValuesSourceType.ANY, valueType);
}
/**
* Read from a stream.
*/
public TermsAggregatorBuilder(StreamInput in) throws IOException {
super(in, StringTerms.TYPE, ValuesSourceType.ANY);
bucketCountThresholds = BucketCountThresholds.readFromStream(in);
collectMode = SubAggCollectionMode.BREADTH_FIRST.readFrom(in);
executionHint = in.readOptionalString();
if (in.readBoolean()) {
includeExclude = IncludeExclude.readFromStream(in);
}
order = InternalOrder.Streams.readOrder(in);
showTermDocCountError = in.readBoolean();
}
@Override
protected boolean serializeTargetValueType() {
return true;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
bucketCountThresholds.writeTo(out);
collectMode.writeTo(out);
out.writeOptionalString(executionHint);
boolean hasIncExc = includeExclude != null;
out.writeBoolean(hasIncExc);
if (hasIncExc) {
includeExclude.writeTo(out);
}
InternalOrder.Streams.writeOrder(order, out);
out.writeBoolean(showTermDocCountError);
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
public TermsAggregator.BucketCountThresholds bucketCountThresholds() {
return bucketCountThresholds;
}
@ -251,35 +284,6 @@ public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Values
return builder;
}
@Override
protected TermsAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {
TermsAggregatorBuilder factory = new TermsAggregatorBuilder(name, targetValueType);
factory.bucketCountThresholds = BucketCountThresholds.readFromStream(in);
factory.collectMode = SubAggCollectionMode.BREADTH_FIRST.readFrom(in);
factory.executionHint = in.readOptionalString();
if (in.readBoolean()) {
factory.includeExclude = IncludeExclude.readFromStream(in);
}
factory.order = InternalOrder.Streams.readOrder(in);
factory.showTermDocCountError = in.readBoolean();
return factory;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
bucketCountThresholds.writeTo(out);
collectMode.writeTo(out);
out.writeOptionalString(executionHint);
boolean hasIncExc = includeExclude != null;
out.writeBoolean(hasIncExc);
if (hasIncExc) {
includeExclude.writeTo(out);
}
InternalOrder.Streams.writeOrder(order, out);
out.writeBoolean(showTermDocCountError);
}
@Override
protected int innerHashCode() {
return Objects.hash(bucketCountThresholds, collectMode, executionHint, includeExclude, order, showTermDocCountError);
@ -296,4 +300,8 @@ public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder<Values
&& Objects.equals(showTermDocCountError, other.showTermDocCountError);
}
@Override
public String getWriteableName() {
return NAME;
}
}

View File

@ -40,13 +40,6 @@ import java.util.Map;
*
*/
public class TermsParser extends AbstractTermsParser {
@Override
public String type() {
return StringTerms.TYPE.name();
}
@Override
protected TermsAggregatorBuilder doCreateFactory(String aggregationName, ValuesSourceType valuesSourceType,
ValueType targetValueType, BucketCountThresholds bucketCountThresholds, SubAggCollectionMode collectMode, String executionHint,
@ -177,10 +170,4 @@ public class TermsParser extends AbstractTermsParser {
}
return Order.aggregation(key, asc);
}
@Override
public TermsAggregatorBuilder getFactoryPrototypes() {
return TermsAggregatorBuilder.PROTOTYPE;
}
}

View File

@ -168,8 +168,7 @@ public enum ValueType implements Writeable<ValueType> {
return description;
}
@Override
public ValueType readFrom(StreamInput in) throws IOException {
public static ValueType readFromStream(StreamInput in) throws IOException {
byte id = in.readByte();
for (ValueType valueType : values()) {
if (id == valueType.id) {

View File

@ -89,19 +89,39 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
}
/**
* Read from a stream.
* Read an aggregation from a stream that does not serialize its targetValueType. This should be used by most subclasses.
*/
protected ValuesSourceAggregatorBuilder(StreamInput in, Type type, ValuesSourceType valuesSourceType, ValueType targetValueType)
throws IOException {
super(in, type);
assert false == serializeTargetValueType() : "Wrong read constructor called for subclass that provides its targetValueType";
this.valuesSourceType = valuesSourceType;
this.targetValueType = targetValueType;
read(in);
}
/**
* Read an aggregation from a stream that serializes its targetValueType. This should only be used by subclasses that override
* {@link #serializeTargetValueType()} to return true.
*/
protected ValuesSourceAggregatorBuilder(StreamInput in, Type type, ValuesSourceType valuesSourceType) throws IOException {
super(in, type);
assert serializeTargetValueType() : "Wrong read constructor called for subclass that serializes its targetValueType";
this.valuesSourceType = valuesSourceType;
this.targetValueType = in.readOptionalWriteable(ValueType::readFromStream);
read(in);
}
/**
* Read from a stream.
*/
private void read(StreamInput in) throws IOException {
field = in.readOptionalString();
if (in.readBoolean()) {
script = Script.readScript(in);
}
if (in.readBoolean()) {
valueType = ValueType.STRING.readFrom(in);
valueType = ValueType.readFromStream(in);
}
format = in.readOptionalString();
missing = in.readGenericValue();
@ -112,7 +132,11 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
@Override
protected final void doWriteTo(StreamOutput out) throws IOException {
if (false == usesNewStyleSerialization()) {
if (usesNewStyleSerialization()) {
if (serializeTargetValueType()) {
out.writeOptionalWriteable(targetValueType);
}
} else {
valuesSourceType.writeTo(out);
boolean hasTargetValueType = targetValueType != null;
out.writeBoolean(hasTargetValueType);
@ -155,7 +179,7 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
ValuesSourceType valuesSourceType = ValuesSourceType.ANY.readFrom(in);
ValueType targetValueType = null;
if (in.readBoolean()) {
targetValueType = ValueType.STRING.readFrom(in);
targetValueType = ValueType.readFromStream(in);
}
ValuesSourceAggregatorBuilder<VS, AB> factory = innerReadFrom(name, valuesSourceType, targetValueType, in);
factory.field = in.readOptionalString();
@ -163,7 +187,7 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
factory.script = Script.readScript(in);
}
if (in.readBoolean()) {
factory.valueType = ValueType.STRING.readFrom(in);
factory.valueType = ValueType.readFromStream(in);
}
factory.format = in.readOptionalString();
factory.missing = in.readGenericValue();
@ -178,6 +202,14 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
throw new UnsupportedOperationException(); // NORELEASE remove when no longer overridden
}
/**
* Should this builder serialize its targetValueType? Defaults to false. All subclasses that override this to true should use the three
* argument read constructor rather than the four argument version.
*/
protected boolean serializeTargetValueType() {
return false;
}
/**
* Sets the field to use for this aggregation.
*/