Skip to content

Commit 36f551b

Browse files
authored
Make ValuesSourceConfig behave like a config object (#57762) (#58012)
1 parent 5138c0c commit 36f551b

File tree

84 files changed

+692
-554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+692
-554
lines changed

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/support/ArrayValuesSourceAggregatorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public Aggregator createInternal(SearchContext searchContext,
5050
HashMap<String, ValuesSource> valuesSources = new HashMap<>();
5151

5252
for (Map.Entry<String, ValuesSourceConfig> config : configs.entrySet()) {
53-
ValuesSource vs = config.getValue().toValuesSource();
54-
if (vs != null) {
55-
valuesSources.put(config.getKey(), vs);
53+
ValuesSourceConfig vsc = config.getValue();
54+
if (vsc.hasValues()) {
55+
valuesSources.put(config.getKey(), vsc.getValuesSource());
5656
}
5757
}
5858
if (valuesSources.isEmpty()) {

modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ public void testNoData() throws Exception {
6161
}
6262
}
6363

64+
public void testUnmapped() throws Exception {
65+
MappedFieldType ft =
66+
new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
67+
ft.setName("field");
68+
69+
try (Directory directory = newDirectory();
70+
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
71+
if (randomBoolean()) {
72+
indexWriter.addDocument(Collections.singleton(new StringField("another_field", "value", Field.Store.NO)));
73+
}
74+
try (IndexReader reader = indexWriter.getReader()) {
75+
IndexSearcher searcher = new IndexSearcher(reader);
76+
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg")
77+
.fields(Collections.singletonList("bogus"));
78+
InternalMatrixStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ft);
79+
assertNull(stats.getStats());
80+
assertFalse(MatrixAggregationInspectionHelper.hasValue(stats));
81+
}
82+
}
83+
}
84+
6485
public void testTwoFields() throws Exception {
6586
String fieldA = "a";
6687
MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);

modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregatorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public InternalAggregation buildEmptyAggregation() {
6868
}
6969

7070
@Override
71-
protected Aggregator doCreateInternal(ValuesSource rawValuesSource,
72-
SearchContext searchContext, Aggregator parent,
71+
protected Aggregator doCreateInternal(SearchContext searchContext, Aggregator parent,
7372
boolean collectsFromSingleBucket,
7473
Map<String, Object> metadata) throws IOException {
7574

75+
ValuesSource rawValuesSource = config.getValuesSource();
7676
if (rawValuesSource instanceof WithOrdinals == false) {
7777
throw new AggregationExecutionException("ValuesSource type " + rawValuesSource.toString() +
7878
"is not supported for aggregation " + this.name());

modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregatorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public InternalAggregation buildEmptyAggregation() {
6969
}
7070

7171
@Override
72-
protected Aggregator doCreateInternal(ValuesSource rawValuesSource,
73-
SearchContext searchContext, Aggregator children,
72+
protected Aggregator doCreateInternal(SearchContext searchContext, Aggregator children,
7473
boolean collectsFromSingleBucket,
7574
Map<String, Object> metadata) throws IOException {
7675

76+
ValuesSource rawValuesSource = config.getValuesSource();
7777
if (rawValuesSource instanceof WithOrdinals == false) {
7878
throw new AggregationExecutionException("ValuesSource type " + rawValuesSource.toString() +
7979
"is not supported for aggregation " + this.name());

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public DateHistogramValuesSourceBuilder offset(long offset) {
249249
@Override
250250
protected CompositeValuesSourceConfig innerBuild(QueryShardContext queryShardContext, ValuesSourceConfig config) throws IOException {
251251
Rounding rounding = dateHistogramInterval.createRounding(timeZone(), offset);
252-
ValuesSource orig = config.toValuesSource();
252+
ValuesSource orig = config.hasValues() ? config.getValuesSource() : null;
253253
if (orig == null) {
254254
orig = ValuesSource.Numeric.EMPTY;
255255
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/GeoTileGridValuesSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean equals(Object obj) {
129129

130130
@Override
131131
protected CompositeValuesSourceConfig innerBuild(QueryShardContext queryShardContext, ValuesSourceConfig config) throws IOException {
132-
ValuesSource orig = config.toValuesSource();
132+
ValuesSource orig = config.hasValues() ? config.getValuesSource() : null;
133133
if (orig == null) {
134134
orig = ValuesSource.GeoPoint.EMPTY;
135135
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/HistogramValuesSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public HistogramValuesSourceBuilder interval(double interval) {
111111

112112
@Override
113113
protected CompositeValuesSourceConfig innerBuild(QueryShardContext queryShardContext, ValuesSourceConfig config) throws IOException {
114-
ValuesSource orig = config.toValuesSource();
114+
ValuesSource orig = config.hasValues() ? config.getValuesSource() : null;
115115
if (orig == null) {
116116
orig = ValuesSource.Numeric.EMPTY;
117117
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/TermsValuesSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public String type() {
7171

7272
@Override
7373
protected CompositeValuesSourceConfig innerBuild(QueryShardContext queryShardContext, ValuesSourceConfig config) throws IOException {
74-
ValuesSource vs = config.toValuesSource();
74+
ValuesSource vs = config.hasValues() ? config.getValuesSource() : null;
7575
if (vs == null) {
7676
// The field is unmapped so we use a value source that can parse any type of values.
7777
// This is needed because the after values are parsed even when there are no values to process.

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridAggregatorFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,17 @@ public InternalAggregation buildEmptyAggregation() {
7474
}
7575

7676
@Override
77-
protected Aggregator doCreateInternal(final ValuesSource valuesSource,
78-
SearchContext searchContext,
79-
Aggregator parent,
80-
boolean collectsFromSingleBucket,
81-
Map<String, Object> metadata) throws IOException {
77+
protected Aggregator doCreateInternal(SearchContext searchContext,
78+
Aggregator parent,
79+
boolean collectsFromSingleBucket,
80+
Map<String, Object> metadata) throws IOException {
8281
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry()
8382
.getAggregator(config, GeoHashGridAggregationBuilder.NAME);
8483
if (aggregatorSupplier instanceof GeoGridAggregatorSupplier == false) {
8584
throw new AggregationExecutionException("Registry miss-match - expected "
8685
+ GeoGridAggregatorSupplier.class.getName() + ", found [" + aggregatorSupplier.getClass().toString() + "]");
8786
}
88-
return ((GeoGridAggregatorSupplier) aggregatorSupplier).build(name, factories, valuesSource, precision, geoBoundingBox,
87+
return ((GeoGridAggregatorSupplier) aggregatorSupplier).build(name, factories, config.getValuesSource(), precision, geoBoundingBox,
8988
requiredSize, shardSize, searchContext, parent, collectsFromSingleBucket, metadata);
9089
}
9190

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridAggregatorFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,17 @@ public InternalAggregation buildEmptyAggregation() {
7272
}
7373

7474
@Override
75-
protected Aggregator doCreateInternal(final ValuesSource valuesSource,
76-
SearchContext searchContext,
77-
Aggregator parent,
78-
boolean collectsFromSingleBucket,
79-
Map<String, Object> metadata) throws IOException {
75+
protected Aggregator doCreateInternal(SearchContext searchContext,
76+
Aggregator parent,
77+
boolean collectsFromSingleBucket,
78+
Map<String, Object> metadata) throws IOException {
8079
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry()
8180
.getAggregator(config, GeoTileGridAggregationBuilder.NAME);
8281
if (aggregatorSupplier instanceof GeoGridAggregatorSupplier == false) {
8382
throw new AggregationExecutionException("Registry miss-match - expected "
8483
+ GeoGridAggregatorSupplier.class.getName() + ", found [" + aggregatorSupplier.getClass().toString() + "]");
8584
}
86-
return ((GeoGridAggregatorSupplier) aggregatorSupplier).build(name, factories, valuesSource, precision, geoBoundingBox,
85+
return ((GeoGridAggregatorSupplier) aggregatorSupplier).build(name, factories, config.getValuesSource(), precision, geoBoundingBox,
8786
requiredSize, shardSize, searchContext, parent, collectsFromSingleBucket, metadata);
8887
}
8988

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregator.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.lucene.index.SortedNumericDocValues;
2323
import org.apache.lucene.search.ScoreMode;
2424
import org.apache.lucene.util.CollectionUtil;
25-
import org.elasticsearch.common.Nullable;
2625
import org.elasticsearch.common.Rounding;
2726
import org.elasticsearch.common.lease.Releasables;
2827
import org.elasticsearch.common.util.LongHash;
@@ -38,6 +37,7 @@
3837
import org.elasticsearch.search.aggregations.bucket.MergingBucketsDeferringCollector;
3938
import org.elasticsearch.search.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder.RoundingInfo;
4039
import org.elasticsearch.search.aggregations.support.ValuesSource;
40+
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
4141
import org.elasticsearch.search.internal.SearchContext;
4242

4343
import java.io.IOException;
@@ -48,7 +48,7 @@
4848
/**
4949
* An aggregator for date values that attempts to return a specific number of
5050
* buckets, reconfiguring how it rounds dates to buckets on the fly as new
51-
* data arrives.
51+
* data arrives.
5252
*/
5353
class AutoDateHistogramAggregator extends DeferableBucketAggregator {
5454

@@ -63,14 +63,23 @@ class AutoDateHistogramAggregator extends DeferableBucketAggregator {
6363
private int targetBuckets;
6464
private MergingBucketsDeferringCollector deferringCollector;
6565

66-
AutoDateHistogramAggregator(String name, AggregatorFactories factories, int numBuckets, RoundingInfo[] roundingInfos,
67-
Function<Rounding, Rounding.Prepared> roundingPreparer, @Nullable ValuesSource valuesSource, DocValueFormat formatter,
68-
SearchContext aggregationContext, Aggregator parent, Map<String, Object> metadata) throws IOException {
66+
AutoDateHistogramAggregator(
67+
String name,
68+
AggregatorFactories factories,
69+
int numBuckets,
70+
RoundingInfo[] roundingInfos,
71+
Function<Rounding, Rounding.Prepared> roundingPreparer,
72+
ValuesSourceConfig valuesSourceConfig,
73+
SearchContext aggregationContext,
74+
Aggregator parent,
75+
Map<String, Object> metadata
76+
) throws IOException {
6977

7078
super(name, factories, aggregationContext, parent, metadata);
7179
this.targetBuckets = numBuckets;
72-
this.valuesSource = (ValuesSource.Numeric) valuesSource;
73-
this.formatter = formatter;
80+
// TODO: Remove null usage here, by using a different aggregator for create
81+
this.valuesSource = valuesSourceConfig.hasValues() ? (ValuesSource.Numeric) valuesSourceConfig.getValuesSource() : null;
82+
this.formatter = valuesSourceConfig.format();
7483
this.roundingInfos = roundingInfos;
7584
this.roundingPreparer = roundingPreparer;
7685
preparedRounding = roundingPreparer.apply(roundingInfos[roundingIdx].rounding);

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorFactory.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.search.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder.RoundingInfo;
2929
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
3030
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
31-
import org.elasticsearch.search.aggregations.support.ValuesSource;
3231
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
3332
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3433
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
@@ -64,11 +63,10 @@ public AutoDateHistogramAggregatorFactory(String name,
6463
}
6564

6665
@Override
67-
protected Aggregator doCreateInternal(ValuesSource valuesSource,
68-
SearchContext searchContext,
69-
Aggregator parent,
70-
boolean collectsFromSingleBucket,
71-
Map<String, Object> metadata) throws IOException {
66+
protected Aggregator doCreateInternal(SearchContext searchContext,
67+
Aggregator parent,
68+
boolean collectsFromSingleBucket,
69+
Map<String, Object> metadata) throws IOException {
7270
if (collectsFromSingleBucket == false) {
7371
return asMultiBucketAggregator(this, searchContext, parent);
7472
}
@@ -79,16 +77,16 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
7977
aggregatorSupplier.getClass().toString() + "]");
8078
}
8179
Function<Rounding, Rounding.Prepared> roundingPreparer =
82-
valuesSource.roundingPreparer(searchContext.getQueryShardContext().getIndexReader());
80+
config.getValuesSource().roundingPreparer(searchContext.getQueryShardContext().getIndexReader());
8381
return ((AutoDateHistogramAggregatorSupplier) aggregatorSupplier).build(name, factories, numBuckets, roundingInfos,
84-
roundingPreparer, valuesSource, config.format(), searchContext, parent, metadata);
82+
roundingPreparer, config, searchContext, parent, metadata);
8583
}
8684

8785
@Override
8886
protected Aggregator createUnmapped(SearchContext searchContext,
8987
Aggregator parent,
9088
Map<String, Object> metadata) throws IOException {
91-
return new AutoDateHistogramAggregator(name, factories, numBuckets, roundingInfos, Rounding::prepareForUnknown, null,
92-
config.format(), searchContext, parent, metadata);
89+
return new AutoDateHistogramAggregator(name, factories, numBuckets, roundingInfos, Rounding::prepareForUnknown,
90+
config, searchContext, parent, metadata);
9391
}
9492
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorSupplier.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121

2222
import org.elasticsearch.common.Nullable;
2323
import org.elasticsearch.common.Rounding;
24-
import org.elasticsearch.search.DocValueFormat;
2524
import org.elasticsearch.search.aggregations.Aggregator;
2625
import org.elasticsearch.search.aggregations.AggregatorFactories;
2726
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
28-
import org.elasticsearch.search.aggregations.support.ValuesSource;
27+
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
2928
import org.elasticsearch.search.internal.SearchContext;
3029

3130
import java.io.IOException;
@@ -41,8 +40,7 @@ Aggregator build(
4140
AutoDateHistogramAggregationBuilder.RoundingInfo[] roundingInfos,
4241
@Nullable
4342
Function<Rounding, Rounding.Prepared> roundingPreparer,
44-
@Nullable ValuesSource valuesSource,
45-
DocValueFormat formatter,
43+
ValuesSourceConfig valuesSourceConfig,
4644
SearchContext aggregationContext,
4745
Aggregator parent,
4846
Map<String, Object> metadata

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationSupplier.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121

2222
import org.elasticsearch.common.Nullable;
2323
import org.elasticsearch.common.Rounding;
24-
import org.elasticsearch.search.DocValueFormat;
2524
import org.elasticsearch.search.aggregations.Aggregator;
2625
import org.elasticsearch.search.aggregations.AggregatorFactories;
2726
import org.elasticsearch.search.aggregations.BucketOrder;
2827
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
29-
import org.elasticsearch.search.aggregations.support.ValuesSource;
28+
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3029
import org.elasticsearch.search.internal.SearchContext;
3130

3231
import java.io.IOException;
@@ -42,8 +41,7 @@ Aggregator build(String name,
4241
boolean keyed,
4342
long minDocCount,
4443
@Nullable ExtendedBounds extendedBounds,
45-
@Nullable ValuesSource valuesSource,
46-
DocValueFormat formatter,
44+
ValuesSourceConfig valuesSourceConfig,
4745
SearchContext aggregationContext,
4846
Aggregator parent,
4947
boolean collectsFromSingleBucket,

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator;
3636
import org.elasticsearch.search.aggregations.bucket.terms.LongKeyedBucketOrds;
3737
import org.elasticsearch.search.aggregations.support.ValuesSource;
38+
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3839
import org.elasticsearch.search.internal.SearchContext;
3940

4041
import java.io.IOException;
@@ -65,11 +66,21 @@ class DateHistogramAggregator extends BucketsAggregator {
6566

6667
private final LongKeyedBucketOrds bucketOrds;
6768

68-
DateHistogramAggregator(String name, AggregatorFactories factories, Rounding rounding, Rounding.Prepared preparedRounding,
69-
BucketOrder order, boolean keyed,
70-
long minDocCount, @Nullable ExtendedBounds extendedBounds, @Nullable ValuesSource valuesSource,
71-
DocValueFormat formatter, SearchContext aggregationContext,
72-
Aggregator parent, boolean collectsFromSingleBucket, Map<String, Object> metadata) throws IOException {
69+
DateHistogramAggregator(
70+
String name,
71+
AggregatorFactories factories,
72+
Rounding rounding,
73+
Rounding.Prepared preparedRounding,
74+
BucketOrder order,
75+
boolean keyed,
76+
long minDocCount,
77+
@Nullable ExtendedBounds extendedBounds,
78+
ValuesSourceConfig valuesSourceConfig,
79+
SearchContext aggregationContext,
80+
Aggregator parent,
81+
boolean collectsFromSingleBucket,
82+
Map<String, Object> metadata
83+
) throws IOException {
7384

7485
super(name, factories, aggregationContext, parent, metadata);
7586
this.rounding = rounding;
@@ -79,8 +90,9 @@ class DateHistogramAggregator extends BucketsAggregator {
7990
this.keyed = keyed;
8091
this.minDocCount = minDocCount;
8192
this.extendedBounds = extendedBounds;
82-
this.valuesSource = (ValuesSource.Numeric) valuesSource;
83-
this.formatter = formatter;
93+
// TODO: Stop using null here
94+
this.valuesSource = valuesSourceConfig.hasValues() ? (ValuesSource.Numeric) valuesSourceConfig.getValuesSource() : null;
95+
this.formatter = valuesSourceConfig.format();
8496

8597
bucketOrds = LongKeyedBucketOrds.build(context.bigArrays(), collectsFromSingleBucket);
8698
}
@@ -129,7 +141,7 @@ public void collect(int doc, long owningBucketOrd) throws IOException {
129141

130142
@Override
131143
public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException {
132-
return buildAggregationsForVariableBuckets(owningBucketOrds, bucketOrds,
144+
return buildAggregationsForVariableBuckets(owningBucketOrds, bucketOrds,
133145
(bucketValue, docCount, subAggregationResults) -> {
134146
return new InternalDateHistogram.Bucket(bucketValue, docCount, keyed, formatter, subAggregationResults);
135147
}, buckets -> {

0 commit comments

Comments
 (0)