20
20
package org .elasticsearch .search .aggregations .metrics ;
21
21
22
22
import org .elasticsearch .index .query .QueryShardContext ;
23
+ import org .elasticsearch .search .DocValueFormat ;
23
24
import org .elasticsearch .search .aggregations .AggregationExecutionException ;
24
25
import org .elasticsearch .search .aggregations .Aggregator ;
25
26
import org .elasticsearch .search .aggregations .AggregatorFactories ;
26
27
import org .elasticsearch .search .aggregations .AggregatorFactory ;
27
28
import org .elasticsearch .search .aggregations .pipeline .PipelineAggregator ;
29
+ import org .elasticsearch .search .aggregations .support .AggregatorSupplier ;
30
+ import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
28
31
import org .elasticsearch .search .aggregations .support .ValuesSource ;
29
32
import org .elasticsearch .search .aggregations .support .ValuesSource .Numeric ;
30
33
import org .elasticsearch .search .aggregations .support .ValuesSourceAggregatorFactory ;
31
34
import org .elasticsearch .search .aggregations .support .ValuesSourceConfig ;
35
+ import org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
32
36
import org .elasticsearch .search .internal .SearchContext ;
33
37
34
38
import java .io .IOException ;
@@ -46,6 +50,22 @@ class SumAggregatorFactory extends ValuesSourceAggregatorFactory {
46
50
super (name , config , queryShardContext , parent , subFactoriesBuilder , metaData );
47
51
}
48
52
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
+
49
69
@ Override
50
70
protected Aggregator createUnmapped (SearchContext searchContext ,
51
71
Aggregator parent ,
@@ -62,10 +82,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
62
82
boolean collectsFromSingleBucket ,
63
83
List <PipelineAggregator > pipelineAggregators ,
64
84
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 () + "]" );
68
91
}
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 );
70
94
}
71
95
}
0 commit comments