Remove some ceremony in agg parsing (#53078) (#53117)

With #50871 aggrgations should now be parsed directly by an
`ObjectParser` or `ConstructingObjectParser` without the need for the
ceremonial `parse` method. This removes 9 of those `parse` methods and
parses the aggregation directly from their `ObjectParser`.
This commit is contained in:
Nik Everett 2020-03-04 13:06:41 -05:00 committed by GitHub
parent a5e82d7fd6
commit 302980e0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 31 additions and 63 deletions

View File

@ -386,14 +386,14 @@ public class SearchModule {
.addResultReader(InternalTDigestPercentileRanks.NAME, InternalTDigestPercentileRanks::new)
.addResultReader(InternalHDRPercentileRanks.NAME, InternalHDRPercentileRanks::new));
registerAggregation(new AggregationSpec(MedianAbsoluteDeviationAggregationBuilder.NAME,
MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder::parse)
MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder.PARSER)
.addResultReader(InternalMedianAbsoluteDeviation::new));
registerAggregation(new AggregationSpec(CardinalityAggregationBuilder.NAME, CardinalityAggregationBuilder::new,
CardinalityAggregationBuilder::parse).addResultReader(InternalCardinality::new));
CardinalityAggregationBuilder.PARSER).addResultReader(InternalCardinality::new));
registerAggregation(new AggregationSpec(GlobalAggregationBuilder.NAME, GlobalAggregationBuilder::new,
GlobalAggregationBuilder::parse).addResultReader(InternalGlobal::new));
registerAggregation(new AggregationSpec(MissingAggregationBuilder.NAME, MissingAggregationBuilder::new,
MissingAggregationBuilder::parse).addResultReader(InternalMissing::new));
MissingAggregationBuilder.PARSER).addResultReader(InternalMissing::new));
registerAggregation(new AggregationSpec(FilterAggregationBuilder.NAME, FilterAggregationBuilder::new,
FilterAggregationBuilder::parse).addResultReader(InternalFilter::new));
registerAggregation(new AggregationSpec(FiltersAggregationBuilder.NAME, FiltersAggregationBuilder::new,
@ -405,16 +405,16 @@ public class SearchModule {
.addResultReader(InternalSampler.NAME, InternalSampler::new)
.addResultReader(UnmappedSampler.NAME, UnmappedSampler::new));
registerAggregation(new AggregationSpec(DiversifiedAggregationBuilder.NAME, DiversifiedAggregationBuilder::new,
DiversifiedAggregationBuilder::parse)
DiversifiedAggregationBuilder.PARSER)
/* Reuses result readers from SamplerAggregator*/);
registerAggregation(new AggregationSpec(TermsAggregationBuilder.NAME, TermsAggregationBuilder::new,
TermsAggregationBuilder::parse)
TermsAggregationBuilder.PARSER)
.addResultReader(StringTerms.NAME, StringTerms::new)
.addResultReader(UnmappedTerms.NAME, UnmappedTerms::new)
.addResultReader(LongTerms.NAME, LongTerms::new)
.addResultReader(DoubleTerms.NAME, DoubleTerms::new));
registerAggregation(new AggregationSpec(RareTermsAggregationBuilder.NAME, RareTermsAggregationBuilder::new,
RareTermsAggregationBuilder::parse)
RareTermsAggregationBuilder.PARSER)
.addResultReader(StringRareTerms.NAME, StringRareTerms::new)
.addResultReader(UnmappedRareTerms.NAME, UnmappedRareTerms::new)
.addResultReader(LongRareTerms.NAME, LongRareTerms::new));

View File

@ -57,15 +57,15 @@ public class AdjacencyMatrixAggregationBuilder extends AbstractAggregationBuilde
private List<KeyedFilter> filters;
private String separator = DEFAULT_SEPARATOR;
private static final ObjectParser<AdjacencyMatrixAggregationBuilder, Void> PARSER = new ObjectParser<>(
AdjacencyMatrixAggregationBuilder.NAME);
private static final ObjectParser<AdjacencyMatrixAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, AdjacencyMatrixAggregationBuilder::new);
static {
PARSER.declareString(AdjacencyMatrixAggregationBuilder::separator, SEPARATOR_FIELD);
PARSER.declareNamedObjects(AdjacencyMatrixAggregationBuilder::setFiltersAsList, KeyedFilter.PARSER, FILTERS_FIELD);
}
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
AdjacencyMatrixAggregationBuilder result = PARSER.parse(parser, new AdjacencyMatrixAggregationBuilder(aggregationName), null);
public static AggregationBuilder parse(XContentParser parser, String name) throws IOException {
AdjacencyMatrixAggregationBuilder result = PARSER.parse(parser, name);
result.checkConsistency();
return result;
}
@ -76,7 +76,6 @@ public class AdjacencyMatrixAggregationBuilder extends AbstractAggregationBuilde
}
}
protected void setFiltersAsMap(Map<String, QueryBuilder> filters) {
// Convert uniquely named objects into internal KeyedFilters
this.filters = new ArrayList<>(filters.size());

View File

@ -61,8 +61,8 @@ public class AdjacencyMatrixAggregator extends BucketsAggregator {
private final String key;
private final QueryBuilder filter;
public static final NamedObjectParser<KeyedFilter, Void> PARSER =
(XContentParser p, Void c, String name) ->
public static final NamedObjectParser<KeyedFilter, String> PARSER =
(XContentParser p, String aggName, String name) ->
new KeyedFilter(name, parseInnerQueryBuilder(p));
public KeyedFilter(String key, QueryBuilder filter) {

View File

@ -106,7 +106,7 @@ public class FilterAggregationBuilder extends AbstractAggregationBuilder<FilterA
return builder;
}
public static FilterAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
public static FilterAggregationBuilder parse(XContentParser parser, String aggregationName) throws IOException {
QueryBuilder filter = parseInnerQueryBuilder(parser);
return new FilterAggregationBuilder(aggregationName, filter);
}

View File

@ -33,6 +33,11 @@ import java.io.IOException;
import java.util.Map;
public class GlobalAggregationBuilder extends AbstractAggregationBuilder<GlobalAggregationBuilder> {
public static GlobalAggregationBuilder parse(XContentParser parser, String aggregationName) throws IOException {
parser.nextToken();
return new GlobalAggregationBuilder(aggregationName);
}
public static final String NAME = "global";
public GlobalAggregationBuilder(String name) {
@ -73,11 +78,6 @@ public class GlobalAggregationBuilder extends AbstractAggregationBuilder<GlobalA
return builder;
}
public static GlobalAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
parser.nextToken();
return new GlobalAggregationBuilder(aggregationName);
}
@Override
public String getType() {
return NAME;

View File

@ -24,7 +24,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
@ -43,16 +42,12 @@ import java.util.Map;
public class MissingAggregationBuilder extends ValuesSourceAggregationBuilder<ValuesSource, MissingAggregationBuilder> {
public static final String NAME = "missing";
private static final ObjectParser<MissingAggregationBuilder, Void> PARSER;
public static final ObjectParser<MissingAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, name -> new MissingAggregationBuilder(name, null));
static {
PARSER = new ObjectParser<>(MissingAggregationBuilder.NAME);
ValuesSourceParserHelper.declareAnyFields(PARSER, true, true);
}
public static MissingAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new MissingAggregationBuilder(aggregationName, null), null);
}
public MissingAggregationBuilder(String name, ValueType targetValueType) {
super(name, CoreValuesSourceType.ANY, targetValueType);
}

View File

@ -23,7 +23,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
@ -44,19 +43,15 @@ public class DiversifiedAggregationBuilder extends ValuesSourceAggregationBuilde
public static final int MAX_DOCS_PER_VALUE_DEFAULT = 1;
private static final ObjectParser<DiversifiedAggregationBuilder, Void> PARSER;
public static final ObjectParser<DiversifiedAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, DiversifiedAggregationBuilder::new);
static {
PARSER = new ObjectParser<>(DiversifiedAggregationBuilder.NAME);
ValuesSourceParserHelper.declareAnyFields(PARSER, true, false);
PARSER.declareInt(DiversifiedAggregationBuilder::shardSize, SamplerAggregator.SHARD_SIZE_FIELD);
PARSER.declareInt(DiversifiedAggregationBuilder::maxDocsPerValue, SamplerAggregator.MAX_DOCS_PER_VALUE_FIELD);
PARSER.declareString(DiversifiedAggregationBuilder::executionHint, SamplerAggregator.EXECUTION_HINT_FIELD);
}
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new DiversifiedAggregationBuilder(aggregationName), null);
}
private int shardSize = SamplerAggregationBuilder.DEFAULT_SHARD_SAMPLE_SIZE;
private int maxDocsPerValue = MAX_DOCS_PER_VALUE_DEFAULT;
private String executionHint = null;

View File

@ -24,7 +24,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
@ -48,9 +47,9 @@ public class RareTermsAggregationBuilder extends ValuesSourceAggregationBuilder<
private static final ParseField PRECISION = new ParseField("precision");
private static final int MAX_MAX_DOC_COUNT = 100;
private static final ObjectParser<RareTermsAggregationBuilder, Void> PARSER;
public static final ObjectParser<RareTermsAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, name -> new RareTermsAggregationBuilder(name, null));
static {
PARSER = new ObjectParser<>(RareTermsAggregationBuilder.NAME);
ValuesSourceParserHelper.declareAnyFields(PARSER, true, true);
PARSER.declareLong(RareTermsAggregationBuilder::maxDocCount, MAX_DOC_COUNT_FIELD_NAME);
@ -63,10 +62,6 @@ public class RareTermsAggregationBuilder extends ValuesSourceAggregationBuilder<
PARSER.declareDouble(RareTermsAggregationBuilder::setPrecision, PRECISION);
}
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new RareTermsAggregationBuilder(aggregationName, null), null);
}
private IncludeExclude includeExclude = null;
private int maxDocCount = 1;
private double precision = 0.001;

View File

@ -25,7 +25,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
@ -65,9 +64,9 @@ public class TermsAggregationBuilder extends ValuesSourceAggregationBuilder<Valu
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");
private static final ObjectParser<TermsAggregationBuilder, Void> PARSER;
public static final ObjectParser<TermsAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, name -> new TermsAggregationBuilder(name, null));
static {
PARSER = new ObjectParser<>(TermsAggregationBuilder.NAME);
ValuesSourceParserHelper.declareAnyFields(PARSER, true, true);
PARSER.declareBoolean(TermsAggregationBuilder::showTermDocCountError,
@ -97,10 +96,6 @@ public class TermsAggregationBuilder extends ValuesSourceAggregationBuilder<Valu
IncludeExclude::parseExclude, IncludeExclude.EXCLUDE_FIELD, ObjectParser.ValueType.STRING_ARRAY);
}
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new TermsAggregationBuilder(aggregationName, null), null);
}
private BucketOrder order = BucketOrder.compound(BucketOrder.count(false)); // automatically adds tie-breaker key asc order
private IncludeExclude includeExclude = null;
private String executionHint = null;

View File

@ -25,7 +25,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
@ -49,18 +48,14 @@ public final class CardinalityAggregationBuilder
private static final ParseField REHASH = new ParseField("rehash").withAllDeprecated("no replacement - values will always be rehashed");
public static final ParseField PRECISION_THRESHOLD_FIELD = new ParseField("precision_threshold");
private static final ObjectParser<CardinalityAggregationBuilder, Void> PARSER;
public static final ObjectParser<CardinalityAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, name -> new CardinalityAggregationBuilder(name, null));
static {
PARSER = new ObjectParser<>(CardinalityAggregationBuilder.NAME);
ValuesSourceParserHelper.declareAnyFields(PARSER, true, false);
PARSER.declareLong(CardinalityAggregationBuilder::precisionThreshold, CardinalityAggregationBuilder.PRECISION_THRESHOLD_FIELD);
PARSER.declareLong((b, v) -> {/*ignore*/}, REHASH);
}
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new CardinalityAggregationBuilder(aggregationName, null), null);
}
private Long precisionThreshold = null;
public CardinalityAggregationBuilder(String name, ValueType targetValueType) {

View File

@ -24,7 +24,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
@ -47,18 +46,13 @@ public class MedianAbsoluteDeviationAggregationBuilder extends LeafOnly<ValuesSo
private static final ParseField COMPRESSION_FIELD = new ParseField("compression");
private static final ObjectParser<MedianAbsoluteDeviationAggregationBuilder, Void> PARSER;
public static final ObjectParser<MedianAbsoluteDeviationAggregationBuilder, String> PARSER =
ObjectParser.fromBuilder(NAME, MedianAbsoluteDeviationAggregationBuilder::new);
static {
PARSER = new ObjectParser<>(NAME);
ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false);
PARSER.declareDouble(MedianAbsoluteDeviationAggregationBuilder::compression, COMPRESSION_FIELD);
}
public static MedianAbsoluteDeviationAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
return PARSER.parse(parser, new MedianAbsoluteDeviationAggregationBuilder(aggregationName), null);
}
private double compression = 1000d;
public MedianAbsoluteDeviationAggregationBuilder(String name) {

View File

@ -154,7 +154,7 @@ public class SearchModuleTests extends ESTestCase {
@Override
public List<AggregationSpec> getAggregations() {
return singletonList(new AggregationSpec(TermsAggregationBuilder.NAME, TermsAggregationBuilder::new,
TermsAggregationBuilder::parse));
TermsAggregationBuilder.PARSER));
}
};
expectThrows(IllegalArgumentException.class, registryForPlugin(registersDupeAggregation));