diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index c06fd014787..1318ac8ae1e 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -133,9 +133,10 @@ import org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGe import org.elasticsearch.search.aggregations.bucket.range.ipv4.IPv4RangeAggregatorBuilder; import org.elasticsearch.search.aggregations.bucket.range.ipv4.InternalIPv4Range; import org.elasticsearch.search.aggregations.bucket.range.ipv4.IpRangeParser; +import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregatorBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedSamplerParser; import org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler; -import org.elasticsearch.search.aggregations.bucket.sampler.SamplerParser; +import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregatorBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.UnmappedSampler; import org.elasticsearch.search.aggregations.bucket.significant.SignificantLongTerms; import org.elasticsearch.search.aggregations.bucket.significant.SignificantStringTerms; @@ -459,8 +460,10 @@ public class SearchModule extends AbstractModule { registerAggregation(FilterAggregatorBuilder::new, FilterAggregatorBuilder::parse, FilterAggregatorBuilder.AGGREGATION_NAME_FIELD); registerAggregation(FiltersAggregatorBuilder::new, (n, c) -> FiltersAggregatorBuilder.parse(queryParserRegistry, n, c), FiltersAggregatorBuilder.AGGREGATION_NAME_FIELD); - registerAggregatorParser(new SamplerParser()); - registerAggregatorParser(new DiversifiedSamplerParser()); + registerAggregation(SamplerAggregatorBuilder::new, SamplerAggregatorBuilder::parse, + SamplerAggregatorBuilder.AGGREGATION_NAME_FIELD); + registerAggregation(DiversifiedAggregatorBuilder::new, new DiversifiedSamplerParser(), + DiversifiedAggregatorBuilder.AGGREGATION_NAME_FIELD); registerAggregation(TermsAggregatorBuilder::new, new TermsParser(), TermsAggregatorBuilder.AGGREGATION_NAME_FIELD); registerAggregatorParser(new SignificantTermsParser(significanceHeuristicParserMapper, queryParserRegistry)); registerAggregation(RangeAggregatorBuilder::new, new RangeParser(), RangeAggregatorBuilder.AGGREGATION_NAME_FIELD); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregatorBuilder.java index d7e44ba8254..a8ff098ccc8 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregatorBuilder.java @@ -19,14 +19,14 @@ package org.elasticsearch.search.aggregations.bucket.sampler; +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.InternalAggregation.Type; 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; @@ -37,10 +37,9 @@ import java.io.IOException; import java.util.Objects; public class DiversifiedAggregatorBuilder extends ValuesSourceAggregatorBuilder { - - public static final Type TYPE = new Type("diversified_sampler"); - - static final DiversifiedAggregatorBuilder PROTOTYPE = new DiversifiedAggregatorBuilder(""); + public static final String NAME = "diversified_sampler"; + public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME); + public static final Type TYPE = new Type(NAME); public static final int MAX_DOCS_PER_VALUE_DEFAULT = 1; @@ -52,6 +51,28 @@ public class DiversifiedAggregatorBuilder extends ValuesSourceAggregatorBuilder< super(name, TYPE, ValuesSourceType.ANY, null); } + /** + * Read from a stream. + */ + public DiversifiedAggregatorBuilder(StreamInput in) throws IOException { + super(in, TYPE, ValuesSourceType.ANY, null); + shardSize = in.readVInt(); + maxDocsPerValue = in.readVInt(); + executionHint = in.readOptionalString(); + } + + @Override + protected void innerWriteTo(StreamOutput out) throws IOException { + out.writeVInt(shardSize); + out.writeVInt(maxDocsPerValue); + out.writeOptionalString(executionHint); + } + + @Override + protected boolean usesNewStyleSerialization() { + return true; + } + /** * Set the max num docs to be returned from each shard. */ @@ -122,23 +143,6 @@ public class DiversifiedAggregatorBuilder extends ValuesSourceAggregatorBuilder< return builder; } - @Override - protected DiversifiedAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType, - ValueType targetValueType, StreamInput in) throws IOException { - DiversifiedAggregatorBuilder factory = new DiversifiedAggregatorBuilder(name); - factory.shardSize = in.readVInt(); - factory.maxDocsPerValue = in.readVInt(); - factory.executionHint = in.readOptionalString(); - return factory; - } - - @Override - protected void innerWriteTo(StreamOutput out) throws IOException { - out.writeVInt(shardSize); - out.writeVInt(maxDocsPerValue); - out.writeOptionalString(executionHint); - } - @Override protected int innerHashCode() { return Objects.hash(shardSize, maxDocsPerValue, executionHint); @@ -151,4 +155,9 @@ public class DiversifiedAggregatorBuilder extends ValuesSourceAggregatorBuilder< && Objects.equals(maxDocsPerValue, other.maxDocsPerValue) && Objects.equals(executionHint, other.executionHint); } + + @Override + public String getWriteableName() { + return NAME; + } } \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerParser.java index 79ac7ba7ed5..cb87e53f2c0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerParser.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.aggregations.bucket.sampler; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.AggregatorBuilder; import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.AnyValuesSourceParser; import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -34,16 +33,10 @@ import java.util.Map; * */ public class DiversifiedSamplerParser extends AnyValuesSourceParser { - public DiversifiedSamplerParser() { super(true, false); } - @Override - public String type() { - return DiversifiedAggregatorBuilder.TYPE.name(); - } - @Override protected DiversifiedAggregatorBuilder createFactory(String aggregationName, ValuesSourceType valuesSourceType, ValueType targetValueType, Map otherOptions) { @@ -85,10 +78,4 @@ public class DiversifiedSamplerParser extends AnyValuesSourceParser { } return false; } - - @Override - public AggregatorBuilder getFactoryPrototypes() { - return DiversifiedAggregatorBuilder.PROTOTYPE; - } - } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorBuilder.java index d00ea80ae29..2f8a3fc29f5 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorBuilder.java @@ -19,20 +19,24 @@ package org.elasticsearch.search.aggregations.bucket.sampler; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; 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.XContentParser; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.AggregatorBuilder; -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 java.io.IOException; import java.util.Objects; public class SamplerAggregatorBuilder extends AggregatorBuilder { - - static final SamplerAggregatorBuilder PROTOTYPE = new SamplerAggregatorBuilder(""); + public static final String NAME = InternalSampler.TYPE.name(); + public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME); public static final int DEFAULT_SHARD_SAMPLE_SIZE = 100; @@ -42,6 +46,24 @@ public class SamplerAggregatorBuilder extends AggregatorBuilder