diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AvgAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AvgAgg.java index f493fbc9402..ba4efb089d5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AvgAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AvgAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.avg; +import java.util.function.Function; -public class AvgAgg extends LeafAgg { +public class AvgAgg extends DefaultAggSourceLeafAgg { public AvgAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(avg(id())); + Function> builder() { + return AggregationBuilders::avg; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/CardinalityAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/CardinalityAgg.java index ccabd73f007..e47026a78c4 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/CardinalityAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/CardinalityAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.cardinality; +import java.util.function.Function; -public class CardinalityAgg extends LeafAgg { +public class CardinalityAgg extends DefaultAggSourceLeafAgg { public CardinalityAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(cardinality(id())); + Function> builder() { + return AggregationBuilders::cardinality; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/DefaultAggSourceLeafAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/DefaultAggSourceLeafAgg.java new file mode 100644 index 00000000000..f073ee3198d --- /dev/null +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/DefaultAggSourceLeafAgg.java @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.sql.querydsl.agg; + +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; + +import java.util.function.Function; + +abstract class DefaultAggSourceLeafAgg extends LeafAgg { + + DefaultAggSourceLeafAgg(String id, AggSource source) { + super(id, source); + } + + @Override + AggregationBuilder toBuilder() { + return source().with(builder().apply(id())); + } + + abstract Function> builder(); +} diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/ExtendedStatsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/ExtendedStatsAgg.java index 9b772adb079..b7e82e2cb1d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/ExtendedStatsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/ExtendedStatsAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats; +import java.util.function.Function; -public class ExtendedStatsAgg extends LeafAgg { +public class ExtendedStatsAgg extends DefaultAggSourceLeafAgg { public ExtendedStatsAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(extendedStats(id())); + Function> builder() { + return AggregationBuilders::extendedStats; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java index 46944ee634a..4af8493efa2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; @@ -27,11 +28,15 @@ public class FilterExistsAgg extends LeafAgg { @Override AggregationBuilder toBuilder() { + QueryBuilder qb; + if (source().fieldName() != null) { - return filter(id(), QueryBuilders.existsQuery(source().fieldName())); + qb = QueryBuilders.existsQuery(source().fieldName()); } else { - return filter(id(), QueryBuilders.scriptQuery(wrapWithIsNotNull(source().script()).toPainless())); + qb = QueryBuilders.scriptQuery(wrapWithIsNotNull(source().script()).toPainless()); } + + return filter(id(), qb); } private static ScriptTemplate wrapWithIsNotNull(ScriptTemplate script) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/LeafAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/LeafAgg.java index 00d36e14dbf..2af4a213b0e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/LeafAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/LeafAgg.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.sql.querydsl.agg; import org.elasticsearch.search.aggregations.AggregationBuilder; -import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; public abstract class LeafAgg extends Agg { @@ -15,9 +14,4 @@ public abstract class LeafAgg extends Agg { } abstract AggregationBuilder toBuilder(); - - @SuppressWarnings("rawtypes") - protected ValuesSourceAggregationBuilder addAggSource(ValuesSourceAggregationBuilder builder) { - return source().with(builder); - } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MaxAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MaxAgg.java index f552f5f9a55..0fac268b7a9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MaxAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MaxAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.max; +import java.util.function.Function; -public class MaxAgg extends LeafAgg { +public class MaxAgg extends DefaultAggSourceLeafAgg { public MaxAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(max(id())); + Function> builder() { + return AggregationBuilders::max; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MedianAbsoluteDeviationAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MedianAbsoluteDeviationAgg.java index 0c6263dd141..a99ecf61d9e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MedianAbsoluteDeviationAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MedianAbsoluteDeviationAgg.java @@ -6,18 +6,19 @@ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.medianAbsoluteDeviation; +import java.util.function.Function; -public class MedianAbsoluteDeviationAgg extends LeafAgg { +public class MedianAbsoluteDeviationAgg extends DefaultAggSourceLeafAgg { public MedianAbsoluteDeviationAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(medianAbsoluteDeviation(id())); + Function> builder() { + return AggregationBuilders::medianAbsoluteDeviation; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MinAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MinAgg.java index b24cd4f5231..15977bf505f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MinAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/MinAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.min; +import java.util.function.Function; -public class MinAgg extends LeafAgg { +public class MinAgg extends DefaultAggSourceLeafAgg { public MinAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(min(id())); + Function> builder() { + return AggregationBuilders::min; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java index 1b331ec31e2..954583070cb 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java @@ -5,13 +5,14 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import java.util.List; +import java.util.function.Function; import static org.elasticsearch.search.aggregations.AggregationBuilders.percentileRanks; -public class PercentileRanksAgg extends LeafAgg { +public class PercentileRanksAgg extends DefaultAggSourceLeafAgg { private final List values; @@ -21,7 +22,7 @@ public class PercentileRanksAgg extends LeafAgg { } @Override - AggregationBuilder toBuilder() { - return addAggSource(percentileRanks(id(), values.stream().mapToDouble(Double::doubleValue).toArray())); + Function> builder() { + return s -> percentileRanks(s, values.stream().mapToDouble(Double::doubleValue).toArray()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java index e244659100c..60413829d47 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java @@ -6,13 +6,14 @@ package org.elasticsearch.xpack.sql.querydsl.agg; import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.metrics.PercentilesAggregationBuilder; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import java.util.List; +import java.util.function.Function; -import static org.elasticsearch.search.aggregations.AggregationBuilders.percentiles; - -public class PercentilesAgg extends LeafAgg { +public class PercentilesAgg extends DefaultAggSourceLeafAgg { private final List percents; @@ -20,11 +21,16 @@ public class PercentilesAgg extends LeafAgg { super(id, source); this.percents = percents; } - + @Override AggregationBuilder toBuilder() { // TODO: look at keyed - PercentilesAggregationBuilder builder = (PercentilesAggregationBuilder) addAggSource(percentiles(id())); + PercentilesAggregationBuilder builder = (PercentilesAggregationBuilder) super.toBuilder(); return builder.percentiles(percents.stream().mapToDouble(Double::doubleValue).toArray()); } -} + + @Override + Function> builder() { + return AggregationBuilders::percentiles; + } +} \ No newline at end of file diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/StatsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/StatsAgg.java index 69b8c5c6a57..591d371f91e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/StatsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/StatsAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.stats; +import java.util.function.Function; -public class StatsAgg extends LeafAgg { +public class StatsAgg extends DefaultAggSourceLeafAgg { public StatsAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(stats(id())); + Function> builder() { + return AggregationBuilders::stats; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/SumAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/SumAgg.java index 645a98ef174..730d0ed1fd7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/SumAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/SumAgg.java @@ -5,18 +5,19 @@ */ package org.elasticsearch.xpack.sql.querydsl.agg; -import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; -import static org.elasticsearch.search.aggregations.AggregationBuilders.sum; +import java.util.function.Function; -public class SumAgg extends LeafAgg { +public class SumAgg extends DefaultAggSourceLeafAgg { public SumAgg(String id, AggSource source) { super(id, source); } @Override - AggregationBuilder toBuilder() { - return addAggSource(sum(id())); + Function> builder() { + return AggregationBuilders::sum; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java index 4d35831b7cd..5ad17742bbc 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java @@ -84,10 +84,12 @@ public class TopHitsAgg extends LeafAgg { TopHitsAggregationBuilder builder = topHits(id()); if (source().fieldName() != null) { - return builder.docValueField(source().fieldName(), SqlDataTypes.format(fieldDataType)).sorts(sortBuilderList).size(1); + builder.docValueField(source().fieldName(), SqlDataTypes.format(fieldDataType)); } else { - return builder.scriptField(id(), source().script().toPainless()).sorts(sortBuilderList).size(1); + builder.scriptField(id(), source().script().toPainless()); } + + return builder.sorts(sortBuilderList).size(1); } @Override