Skip to content

Commit 59ae37a

Browse files
authored
ValuesSource refactoring: Wire up Sum aggregation (#52571)
1 parent 24ca419 commit 59ae37a

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ private void registerAggregations(List<SearchPlugin> plugins) {
343343
registerAggregation(new AggregationSpec(WeightedAvgAggregationBuilder.NAME, WeightedAvgAggregationBuilder::new,
344344
WeightedAvgAggregationBuilder::parse).addResultReader(InternalWeightedAvg::new));
345345
registerAggregation(new AggregationSpec(SumAggregationBuilder.NAME, SumAggregationBuilder::new, SumAggregationBuilder::parse)
346-
.addResultReader(InternalSum::new));
346+
.addResultReader(InternalSum::new)
347+
.setAggregatorRegistrar(SumAggregationBuilder::registerAggregators));
347348
registerAggregation(new AggregationSpec(MinAggregationBuilder.NAME, MinAggregationBuilder::new, MinAggregationBuilder::parse)
348349
.addResultReader(InternalMin::new)
349350
.setAggregatorRegistrar(MinAggregationBuilder::registerAggregators));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.search.aggregations.metrics;
20+
21+
import org.elasticsearch.search.DocValueFormat;
22+
import org.elasticsearch.search.aggregations.Aggregator;
23+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
24+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
25+
import org.elasticsearch.search.aggregations.support.ValuesSource;
26+
import org.elasticsearch.search.internal.SearchContext;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public interface MetricAggregatorSupplier extends AggregatorSupplier {
33+
34+
Aggregator build(String name,
35+
ValuesSource valuesSource,
36+
DocValueFormat formatter,
37+
SearchContext searchContext,
38+
Aggregator parent,
39+
List<PipelineAggregator> pipelineAggregators,
40+
Map<String, Object> metaData) throws IOException;
41+
}

server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
3434
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3535
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
36+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3637
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
3738

3839
import java.io.IOException;
@@ -51,6 +52,10 @@ public static AggregationBuilder parse(String aggregationName, XContentParser pa
5152
return PARSER.parse(parser, new SumAggregationBuilder(aggregationName), null);
5253
}
5354

55+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
56+
SumAggregatorFactory.registerAggregators(valuesSourceRegistry);
57+
}
58+
5459
public SumAggregationBuilder(String name) {
5560
super(name);
5661
}

server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
package org.elasticsearch.search.aggregations.metrics;
2121

2222
import org.elasticsearch.index.query.QueryShardContext;
23+
import org.elasticsearch.search.DocValueFormat;
2324
import org.elasticsearch.search.aggregations.AggregationExecutionException;
2425
import org.elasticsearch.search.aggregations.Aggregator;
2526
import org.elasticsearch.search.aggregations.AggregatorFactories;
2627
import org.elasticsearch.search.aggregations.AggregatorFactory;
2728
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
29+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
30+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
2831
import org.elasticsearch.search.aggregations.support.ValuesSource;
2932
import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric;
3033
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
3134
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
35+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3236
import org.elasticsearch.search.internal.SearchContext;
3337

3438
import java.io.IOException;
@@ -46,6 +50,22 @@ class SumAggregatorFactory extends ValuesSourceAggregatorFactory {
4650
super(name, config, queryShardContext, parent, subFactoriesBuilder, metaData);
4751
}
4852

53+
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
54+
valuesSourceRegistry.register(SumAggregationBuilder.NAME,
55+
List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
56+
new MetricAggregatorSupplier() {
57+
@Override
58+
public Aggregator build(String name,
59+
ValuesSource valuesSource,
60+
DocValueFormat formatter,
61+
SearchContext context,
62+
Aggregator parent,
63+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
64+
return new SumAggregator(name, (Numeric) valuesSource, formatter, context, parent, pipelineAggregators, metaData);
65+
}
66+
});
67+
}
68+
4969
@Override
5070
protected Aggregator createUnmapped(SearchContext searchContext,
5171
Aggregator parent,
@@ -62,10 +82,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
6282
boolean collectsFromSingleBucket,
6383
List<PipelineAggregator> pipelineAggregators,
6484
Map<String, Object> metaData) throws IOException {
65-
if (valuesSource instanceof Numeric == false) {
66-
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
67-
this.name());
85+
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
86+
SumAggregationBuilder.NAME);
87+
88+
if (aggregatorSupplier instanceof MetricAggregatorSupplier == false) {
89+
throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" +
90+
aggregatorSupplier.getClass().toString() + "]");
6891
}
69-
return new SumAggregator(name, (Numeric) valuesSource, config.format(), searchContext, parent, pipelineAggregators, metaData);
92+
return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), searchContext, parent,
93+
pipelineAggregators, metaData);
7094
}
7195
}

0 commit comments

Comments
 (0)