diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceParserHelper.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceParserHelper.java index e7b152de93266..3328e95527949 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceParserHelper.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceParserHelper.java @@ -26,9 +26,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.AbstractObjectParser; import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.support.ValueType; @@ -37,6 +37,7 @@ import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; public class CompositeValuesSourceParserHelper { + static , T> void declareValuesSourceFields(AbstractObjectParser objectParser, ValueType expectedValueType) { objectParser.declareField(VB::field, XContentParser::text, @@ -44,7 +45,7 @@ static , T> void declareValuesSource objectParser.declareBoolean(VB::missingBucket, new ParseField("missing_bucket")); objectParser.declareField(VB::valueType, p -> { - ValueType valueType = ValueType.resolveForScript(p.text()); + ValueType valueType = ValueType.lenientParse(p.text()); if (expectedValueType != null && valueType.isNotA(expectedValueType)) { throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregationBuilder.java index 57a4ce6fff99e..df6cc702b872b 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregationBuilder.java @@ -38,7 +38,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -65,7 +64,7 @@ protected interface PrecisionParser { public static ObjectParser createParser(String name, PrecisionParser precisionParser) { ObjectParser parser = new ObjectParser<>(name); - ValuesSourceParserHelper.declareGeoFields(parser, false, false); + ValuesSourceAggregationBuilder.declareFields(parser, false, false, false); parser.declareField((p, builder, context) -> builder.precision(precisionParser.parse(p)), FIELD_PRECISION, org.elasticsearch.common.xcontent.ObjectParser.ValueType.INT); parser.declareInt(GeoGridAggregationBuilder::size, FIELD_SIZE); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregationBuilder.java index 8acb50d78a99e..7d299263b3eb0 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregationBuilder.java @@ -38,7 +38,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -60,7 +59,7 @@ public class AutoDateHistogramAggregationBuilder private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(AutoDateHistogramAggregationBuilder.NAME); - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, true); PARSER.declareInt(AutoDateHistogramAggregationBuilder::setNumBuckets, NUM_BUCKETS_FIELD); PARSER.declareStringOrNull(AutoDateHistogramAggregationBuilder::setMinimumIntervalExpression, MINIMUM_INTERVAL_FIELD); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java index 4b6ee93a753e8..0ce5839847652 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java @@ -49,7 +49,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -94,7 +93,7 @@ public class DateHistogramAggregationBuilder extends ValuesSourceAggregationBuil private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(DateHistogramAggregationBuilder.NAME); - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, true); DateIntervalWrapper.declareIntervalFields(PARSER); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java index b8e11d0c44eab..4c164abc5a0e5 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java @@ -37,7 +37,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -65,7 +64,7 @@ public class HistogramAggregationBuilder extends ValuesSourceAggregationBuilder< private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(HistogramAggregationBuilder.NAME); - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareDouble(HistogramAggregationBuilder::interval, Histogram.INTERVAL_FIELD); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java index 059acb6a7a49f..d0a631240367c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -44,7 +43,7 @@ public class MissingAggregationBuilder extends ValuesSourceAggregationBuilder PARSER = ObjectParser.fromBuilder(NAME, MissingAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public MissingAggregationBuilder(String name) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java index c6590b42ff32b..4d155e8e7564d 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java @@ -28,8 +28,8 @@ import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -43,7 +43,7 @@ public class DateRangeAggregationBuilder extends AbstractRangeBuilder PARSER; static { PARSER = new ObjectParser<>(DateRangeAggregationBuilder.NAME); - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, true); PARSER.declareBoolean(DateRangeAggregationBuilder::keyed, RangeAggregator.KEYED_FIELD); PARSER.declareObjectArray((agg, ranges) -> { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/GeoDistanceAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/GeoDistanceAggregationBuilder.java index a3a3779594b77..0d85cebce3069 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/GeoDistanceAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/GeoDistanceAggregationBuilder.java @@ -39,7 +39,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -61,7 +60,7 @@ public class GeoDistanceAggregationBuilder extends ValuesSourceAggregationBuilde private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(GeoDistanceAggregationBuilder.NAME); - ValuesSourceParserHelper.declareGeoFields(PARSER, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, false, false); PARSER.declareBoolean(GeoDistanceAggregationBuilder::keyed, RangeAggregator.KEYED_FIELD); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregationBuilder.java index 6a5d6aeb963db..e3a8ab712b338 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregationBuilder.java @@ -40,7 +40,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -61,7 +60,7 @@ public final class IpRangeAggregationBuilder private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(IpRangeAggregationBuilder.NAME); - ValuesSourceParserHelper.declareBytesFields(PARSER, false, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, false, false, false); PARSER.declareBoolean(IpRangeAggregationBuilder::keyed, RangeAggregator.KEYED_FIELD); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java index 2a679a0161d18..bf030e81ebf7f 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java @@ -28,8 +28,8 @@ import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.Range; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import java.io.IOException; @@ -41,7 +41,7 @@ public class RangeAggregationBuilder extends AbstractRangeBuilder PARSER; static { PARSER = new ObjectParser<>(RangeAggregationBuilder.NAME); - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareBoolean(RangeAggregationBuilder::keyed, RangeAggregator.KEYED_FIELD); PARSER.declareObjectArray((agg, ranges) -> { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregationBuilder.java index 598ce25bb231a..124e4e017dcd7 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -46,7 +45,7 @@ public class DiversifiedAggregationBuilder extends ValuesSourceAggregationBuilde public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, DiversifiedAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, false, 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); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java index c7ef5a1701dc4..702f929a2fe2b 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java @@ -42,7 +42,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -67,7 +66,7 @@ public class SignificantTermsAggregationBuilder extends ValuesSourceAggregationB SignificantTermsAggregationBuilder.NAME, SignificanceHeuristic.class, SignificantTermsAggregationBuilder::significanceHeuristic, null); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareInt(SignificantTermsAggregationBuilder::shardSize, TermsAggregationBuilder.SHARD_SIZE_FIELD_NAME); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregationBuilder.java index 824088bb006d5..c6c1ccce246cc 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregationBuilder.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -50,7 +49,7 @@ public class RareTermsAggregationBuilder extends ValuesSourceAggregationBuilder< public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, RareTermsAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareLong(RareTermsAggregationBuilder::maxDocCount, MAX_DOC_COUNT_FIELD_NAME); PARSER.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(v, b.includeExclude())), diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregationBuilder.java index d9c944e0507bd..18a902f7a4dc1 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregationBuilder.java @@ -40,7 +40,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -67,7 +66,7 @@ public class TermsAggregationBuilder extends ValuesSourceAggregationBuilder PARSER = ObjectParser.fromBuilder(NAME, TermsAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareBoolean(TermsAggregationBuilder::showTermDocCountError, TermsAggregationBuilder.SHOW_TERM_DOC_COUNT_ERROR); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesAggregationBuilder.java index 12a5fdd291181..b9cbc3474a04d 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesAggregationBuilder.java @@ -30,7 +30,6 @@ import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import java.io.IOException; import java.util.Arrays; @@ -105,7 +104,7 @@ public static > ConstructingO return ctor.apply(name, values, percentilesConfig); }); - ValuesSourceParserHelper.declareAnyFields(parser, true, true); + ValuesSourceAggregationBuilder.declareFields(parser, true, true, false); parser.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), valuesField); parser.declareBoolean(T::keyed, KEYED_FIELD); parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), PercentilesMethod.TDIGEST_PARSER, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregationBuilder.java index 68e268958b1f3..be747ab1067ec 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -43,7 +42,7 @@ public class AvgAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOn public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, AvgAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregationBuilder.java index 6525f9e176a71..f67e8d3fe50d4 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregationBuilder.java @@ -33,7 +33,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -52,7 +51,7 @@ public final class CardinalityAggregationBuilder public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, CardinalityAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, false, false); PARSER.declareLong(CardinalityAggregationBuilder::precisionThreshold, CardinalityAggregationBuilder.PRECISION_THRESHOLD_FIELD); PARSER.declareLong((b, v) -> {/*ignore*/}, REHASH); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregationBuilder.java index 2f09067b20390..94d9ec9b8931a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -45,7 +44,7 @@ public class ExtendedStatsAggregationBuilder public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, ExtendedStatsAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareDouble(ExtendedStatsAggregationBuilder::sigma, ExtendedStatsAggregator.SIGMA_FIELD); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregationBuilder.java index 835ca71bffb1d..f22ac70c008c1 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -45,7 +44,7 @@ public class GeoBoundsAggregationBuilder extends ValuesSourceAggregationBuilder< private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(GeoBoundsAggregationBuilder.NAME); - ValuesSourceParserHelper.declareGeoFields(PARSER, false, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, false, false, false); PARSER.declareBoolean(GeoBoundsAggregationBuilder::wrapLongitude, GeoBoundsAggregator.WRAP_LONGITUDE_FIELD); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregationBuilder.java index 94c9ece4b499d..650fc84300342 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregationBuilder.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -46,7 +45,7 @@ public class GeoCentroidAggregationBuilder private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>(GeoCentroidAggregationBuilder.NAME); - ValuesSourceParserHelper.declareGeoFields(PARSER, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, false, false); } public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregationBuilder.java index 977da7b2f94f0..81bf8bf9f63e0 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -43,7 +42,7 @@ public class MaxAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOn public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, MaxAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java index 01c123f5d9fe0..335d1d42ae3bf 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java @@ -30,10 +30,10 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.LeafOnly; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -50,7 +50,7 @@ public class MedianAbsoluteDeviationAggregationBuilder extends LeafOnly PARSER = ObjectParser.fromBuilder(NAME, MedianAbsoluteDeviationAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareDouble(MedianAbsoluteDeviationAggregationBuilder::compression, COMPRESSION_FIELD); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregationBuilder.java index facfa16ff1ed1..820764b3f6ec2 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -43,7 +42,7 @@ public class MinAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOn public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, MinAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public MinAggregationBuilder(String name) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java index cae295f12840b..e169c3021c87c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -42,9 +41,9 @@ public class StatsAggregationBuilder extends ValuesSourceAggregationBuilder.Leaf public static final String NAME = "stats"; public static final ObjectParser PARSER = - ObjectParser.fromBuilder(NAME, StatsAggregationBuilder::new); + ObjectParser.fromBuilder(NAME, StatsAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public StatsAggregationBuilder(String name) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregationBuilder.java index f09740c585a50..02c44597c54a6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregationBuilder.java @@ -31,7 +31,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -43,7 +42,7 @@ public class SumAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOn public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, SumAggregationBuilder::new); static { - ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregationBuilder.java index 2f54635040d6e..fdcd7e452fceb 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregationBuilder.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -45,7 +44,7 @@ public class ValueCountAggregationBuilder extends ValuesSourceAggregationBuilder public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, ValueCountAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); } public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java index d7f38cfa16e08..e6064cbd1f35d 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java @@ -42,7 +42,7 @@ * {@link CoreValuesSourceType} holds the {@link ValuesSourceType} implementations for the core aggregations package. */ public enum CoreValuesSourceType implements ValuesSourceType { - NUMERIC(EquivalenceType.NUMBER) { + NUMERIC() { @Override public ValuesSource getEmpty() { return ValuesSource.Numeric.EMPTY; @@ -76,7 +76,7 @@ public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, return MissingValues.replaceMissing((ValuesSource.Numeric) valuesSource, missing); } }, - BYTES(EquivalenceType.STRING) { + BYTES() { @Override public ValuesSource getEmpty() { return ValuesSource.Bytes.WithOrdinals.EMPTY; @@ -113,7 +113,7 @@ public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, } } }, - GEOPOINT(EquivalenceType.GEO) { + GEOPOINT() { @Override public ValuesSource getEmpty() { return ValuesSource.GeoPoint.EMPTY; @@ -147,7 +147,7 @@ public DocValueFormat getFormatter(String format, ZoneId tz) { return DocValueFormat.GEOHASH; } }, - RANGE(EquivalenceType.RANGE) { + RANGE() { @Override public ValuesSource getEmpty() { // TODO: Is this the correct exception type here? @@ -176,8 +176,7 @@ public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, throw new IllegalArgumentException("Can't apply missing values on a " + valuesSource.getClass()); } }, - // TODO: Ordinal Numbering sync with types from master - IP(EquivalenceType.STRING) { + IP() { @Override public ValuesSource getEmpty() { return BYTES.getEmpty(); @@ -203,7 +202,7 @@ public DocValueFormat getFormatter(String format, ZoneId tz) { return DocValueFormat.IP; } }, - DATE(EquivalenceType.NUMBER) { + DATE() { @Override public ValuesSource getEmpty() { return NUMERIC.getEmpty(); @@ -234,7 +233,7 @@ public DocValueFormat getFormatter(String format, ZoneId tz) { DateFieldMapper.Resolution.MILLISECONDS); } }, - BOOLEAN(EquivalenceType.NUMBER) { + BOOLEAN() { @Override public ValuesSource getEmpty() { return NUMERIC.getEmpty(); @@ -262,24 +261,6 @@ public DocValueFormat getFormatter(String format, ZoneId tz) { } ; - enum EquivalenceType { - STRING, NUMBER, GEO, RANGE; - } - - EquivalenceType equivalenceType; - - CoreValuesSourceType(EquivalenceType equivalenceType) { - this.equivalenceType = equivalenceType; - } - @Override - public boolean isCastableTo(ValuesSourceType valuesSourceType) { - if (valuesSourceType instanceof CoreValuesSourceType == false) { - return false; - } - CoreValuesSourceType other = (CoreValuesSourceType) valuesSourceType; - return this.equivalenceType == other.equivalenceType; - } - public static ValuesSourceType fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/MultiValuesSourceParseHelper.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/MultiValuesSourceParseHelper.java index 3bbb8bfb702fc..bafa6426480be 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/MultiValuesSourceParseHelper.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/MultiValuesSourceParseHelper.java @@ -27,12 +27,12 @@ public final class MultiValuesSourceParseHelper { - public static void declareCommon( + public static void declareCommon( AbstractObjectParser, T> objectParser, boolean formattable, ValueType expectedValueType) { objectParser.declareField(MultiValuesSourceAggregationBuilder::userValueTypeHint, p -> { - ValueType valueType = ValueType.resolveForScript(p.text()); + ValueType valueType = ValueType.lenientParse(p.text()); if (expectedValueType != null && valueType.isNotA(expectedValueType)) { throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java index c2fbab66028ce..068790030bd49 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.time.ZoneOffset; +import java.util.Set; public enum ValueType implements Writeable { @@ -72,8 +73,26 @@ public ValuesSourceType getValuesSourceType() { return valuesSourceType; } + private static Set numericValueTypes = Set.of(ValueType.DOUBLE, ValueType.DATE, ValueType.LONG, ValueType.NUMBER, + ValueType.NUMERIC, ValueType.BOOLEAN); + private static Set stringValueTypes = Set.of(ValueType.STRING, ValueType.IP); + + /** + * This is a bit of a hack to mirror the old {@link ValueType} behavior, which would allow a rough compatibility between types. This + * behavior is being phased out in the aggregations framework, in favor of explicitly listing supported types, but we haven't gotten + * to fixing composite yet. + * + * @param valueType The value type the user suggested + * @return True iff the two value types are interchangeable + */ public boolean isA(ValueType valueType) { - return valueType.valuesSourceType.isCastableTo(valuesSourceType); + if (numericValueTypes.contains(this)) { + return numericValueTypes.contains(valueType); + } + if (stringValueTypes.contains(this)) { + return stringValueTypes.contains(valueType); + } + return this.equals(valueType); } public boolean isNotA(ValueType valueType) { @@ -84,7 +103,7 @@ public DocValueFormat defaultFormat() { return defaultFormat; } - public static ValueType resolveForScript(String type) { + public static ValueType lenientParse(String type) { switch (type) { case "string": return STRING; case "double": diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.java index 6e6213938c754..f7bdecfa3e448 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.java @@ -19,9 +19,13 @@ package org.elasticsearch.search.aggregations.support; import org.elasticsearch.Version; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.AbstractObjectParser; +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.script.Script; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; @@ -31,12 +35,49 @@ import java.io.IOException; import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.Map; import java.util.Objects; public abstract class ValuesSourceAggregationBuilder> extends AbstractAggregationBuilder { + public static void declareFields( + AbstractObjectParser, T> objectParser, + boolean scriptable, boolean formattable, boolean timezoneAware) { + + + objectParser.declareField(ValuesSourceAggregationBuilder::field, XContentParser::text, + ParseField.CommonFields.FIELD, ObjectParser.ValueType.STRING); + + objectParser.declareField(ValuesSourceAggregationBuilder::missing, XContentParser::objectText, + ParseField.CommonFields.MISSING, ObjectParser.ValueType.VALUE); + + objectParser.declareField(ValuesSourceAggregationBuilder::userValueTypeHint, p -> ValueType.lenientParse(p.text()), + ValueType.VALUE_TYPE, ObjectParser.ValueType.STRING); + + if (formattable) { + objectParser.declareField(ValuesSourceAggregationBuilder::format, XContentParser::text, + ParseField.CommonFields.FORMAT, ObjectParser.ValueType.STRING); + } + + if (scriptable) { + objectParser.declareField(ValuesSourceAggregationBuilder::script, + (parser, context) -> Script.parse(parser), + Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING); + } + + if (timezoneAware) { + objectParser.declareField(ValuesSourceAggregationBuilder::timeZone, p -> { + if (p.currentToken() == XContentParser.Token.VALUE_STRING) { + return ZoneId.of(p.text()); + } else { + return ZoneOffset.ofHours(p.intValue()); + } + }, ParseField.CommonFields.TIME_ZONE, ObjectParser.ValueType.LONG); + } + } + public abstract static class LeafOnly> extends ValuesSourceAggregationBuilder { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceParserHelper.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceParserHelper.java deleted file mode 100644 index cd037df16a743..0000000000000 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceParserHelper.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.support; - -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.xcontent.AbstractObjectParser; -import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.script.Script; - -import java.time.ZoneId; -import java.time.ZoneOffset; - -public final class ValuesSourceParserHelper { - - private ValuesSourceParserHelper() {} // utility class, no instantiation - - public static void declareAnyFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable) { - declareAnyFields(objectParser, scriptable, formattable, false); - } - - public static void declareAnyFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable, boolean timezoneAware) { - declareFields(objectParser, scriptable, formattable, timezoneAware, null); - } - - public static void declareNumericFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable, boolean timezoneAware) { - declareFields(objectParser, scriptable, formattable, timezoneAware, ValueType.NUMERIC); - } - - public static void declareBytesFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable) { - declareFields(objectParser, scriptable, formattable, false, ValueType.STRING); - } - - public static void declareGeoFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable) { - declareFields(objectParser, scriptable, formattable, false, ValueType.GEOPOINT); - } - - private static void declareFields( - AbstractObjectParser, T> objectParser, - boolean scriptable, boolean formattable, boolean timezoneAware, ValueType expectedInputType) { - - - objectParser.declareField(ValuesSourceAggregationBuilder::field, XContentParser::text, - ParseField.CommonFields.FIELD, ObjectParser.ValueType.STRING); - - objectParser.declareField(ValuesSourceAggregationBuilder::missing, XContentParser::objectText, - ParseField.CommonFields.MISSING, ObjectParser.ValueType.VALUE); - - objectParser.declareField(ValuesSourceAggregationBuilder::userValueTypeHint, p -> { - ValueType valueType = ValueType.resolveForScript(p.text()); - if (expectedInputType != null && valueType.isNotA(expectedInputType)) { - throw new ParsingException(p.getTokenLocation(), - "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" - + valueType + "]. It can only work on value off type [" - + expectedInputType + "]"); - } - return valueType; - }, ValueType.VALUE_TYPE, ObjectParser.ValueType.STRING); - - if (formattable) { - objectParser.declareField(ValuesSourceAggregationBuilder::format, XContentParser::text, - ParseField.CommonFields.FORMAT, ObjectParser.ValueType.STRING); - } - - if (scriptable) { - objectParser.declareField(ValuesSourceAggregationBuilder::script, - (parser, context) -> Script.parse(parser), - Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING); - } - - if (timezoneAware) { - objectParser.declareField(ValuesSourceAggregationBuilder::timeZone, p -> { - if (p.currentToken() == XContentParser.Token.VALUE_STRING) { - return ZoneId.of(p.text()); - } else { - return ZoneOffset.ofHours(p.intValue()); - } - }, ParseField.CommonFields.TIME_ZONE, ObjectParser.ValueType.LONG); - } - } - - - -} diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceType.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceType.java index d18438827bbc4..040ede186c7ab 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceType.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceType.java @@ -85,7 +85,4 @@ ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, DocVal default DocValueFormat getFormatter(String format, ZoneId tz) { return DocValueFormat.RAW; } - - // TODO: This is scaffolding to shore up the parser logic while we're still refactoring. Don't merge this to master - boolean isCastableTo(ValuesSourceType valuesSourceType); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java index 55ae7deb52a04..9bc7eb9a22988 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java @@ -24,18 +24,18 @@ public class ValueTypeTests extends ESTestCase { public void testResolve() { - assertEquals(ValueType.STRING, ValueType.resolveForScript("string")); - assertEquals(ValueType.DOUBLE, ValueType.resolveForScript("float")); - assertEquals(ValueType.DOUBLE, ValueType.resolveForScript("double")); - assertEquals(ValueType.LONG, ValueType.resolveForScript("byte")); - assertEquals(ValueType.LONG, ValueType.resolveForScript("short")); - assertEquals(ValueType.LONG, ValueType.resolveForScript("integer")); - assertEquals(ValueType.LONG, ValueType.resolveForScript("long")); - assertEquals(ValueType.DATE, ValueType.resolveForScript("date")); - assertEquals(ValueType.IP, ValueType.resolveForScript("ip")); - assertEquals(ValueType.BOOLEAN, ValueType.resolveForScript("boolean")); + assertEquals(ValueType.STRING, ValueType.lenientParse("string")); + assertEquals(ValueType.DOUBLE, ValueType.lenientParse("float")); + assertEquals(ValueType.DOUBLE, ValueType.lenientParse("double")); + assertEquals(ValueType.LONG, ValueType.lenientParse("byte")); + assertEquals(ValueType.LONG, ValueType.lenientParse("short")); + assertEquals(ValueType.LONG, ValueType.lenientParse("integer")); + assertEquals(ValueType.LONG, ValueType.lenientParse("long")); + assertEquals(ValueType.DATE, ValueType.lenientParse("date")); + assertEquals(ValueType.IP, ValueType.lenientParse("ip")); + assertEquals(ValueType.BOOLEAN, ValueType.lenientParse("boolean")); } - + public void testCompatibility() { assertTrue(ValueType.DOUBLE.isA(ValueType.NUMERIC)); assertTrue(ValueType.DOUBLE.isA(ValueType.NUMBER)); diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/support/AnalyticsValuesSourceType.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/support/AnalyticsValuesSourceType.java index 7c4bbfd8da13d..8ff07fda257b5 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/support/AnalyticsValuesSourceType.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/support/AnalyticsValuesSourceType.java @@ -10,7 +10,6 @@ import org.elasticsearch.script.AggregationScript; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; -import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.FieldContext; import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; @@ -50,17 +49,6 @@ public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, }; - @Override - public boolean isCastableTo(ValuesSourceType valuesSourceType) { - // TODO right now only have to care about Histo, but in future we may - // have types that are castable to CoreValuesSourceTypes - if (valuesSourceType instanceof CoreValuesSourceType) { - return false; - } - AnalyticsValuesSourceType other = (AnalyticsValuesSourceType) valuesSourceType; - return other.value().equals(AnalyticsValuesSourceType.HISTOGRAM.value()); - } - public static ValuesSourceType fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregationBuilder.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregationBuilder.java index e5b9a5b839754..677b6d0cf4223 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregationBuilder.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregationBuilder.java @@ -19,7 +19,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -36,7 +35,7 @@ public class BoxplotAggregationBuilder extends ValuesSourceAggregationBuilder.Le public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, BoxplotAggregationBuilder::new); static { - ValuesSourceParserHelper.declareAnyFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareDouble(BoxplotAggregationBuilder::compression, COMPRESSION_FIELD); } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregationBuilder.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregationBuilder.java index 065209efd8ee5..e12cd047234eb 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregationBuilder.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregationBuilder.java @@ -17,7 +17,6 @@ import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -33,7 +32,7 @@ public class StringStatsAggregationBuilder extends ValuesSourceAggregationBuilde public static final ObjectParser PARSER = ObjectParser.fromBuilder(NAME, StringStatsAggregationBuilder::new); static { - ValuesSourceParserHelper.declareBytesFields(PARSER, true, true); + ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false); PARSER.declareBoolean(StringStatsAggregationBuilder::showDistribution, StringStatsAggregationBuilder.SHOW_DISTRIBUTION_FIELD); }