2020package  org .elasticsearch .search .aggregations .metrics ;
2121
2222import  org .elasticsearch .index .query .QueryShardContext ;
23+ import  org .elasticsearch .search .DocValueFormat ;
2324import  org .elasticsearch .search .aggregations .AggregationExecutionException ;
2425import  org .elasticsearch .search .aggregations .Aggregator ;
2526import  org .elasticsearch .search .aggregations .AggregatorFactories ;
2627import  org .elasticsearch .search .aggregations .AggregatorFactory ;
2728import  org .elasticsearch .search .aggregations .pipeline .PipelineAggregator ;
29+ import  org .elasticsearch .search .aggregations .support .AggregatorSupplier ;
30+ import  org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
2831import  org .elasticsearch .search .aggregations .support .ValuesSource ;
2932import  org .elasticsearch .search .aggregations .support .ValuesSource .Numeric ;
3033import  org .elasticsearch .search .aggregations .support .ValuesSourceAggregatorFactory ;
3134import  org .elasticsearch .search .aggregations .support .ValuesSourceConfig ;
35+ import  org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
3236import  org .elasticsearch .search .internal .SearchContext ;
3337
3438import  java .io .IOException ;
@@ -43,6 +47,22 @@ class AvgAggregatorFactory extends ValuesSourceAggregatorFactory {
4347        super (name , config , queryShardContext , parent , subFactoriesBuilder , metaData );
4448    }
4549
50+     static  void  registerAggregators (ValuesSourceRegistry  valuesSourceRegistry ) {
51+         valuesSourceRegistry .register (AvgAggregationBuilder .NAME ,
52+             List .of (CoreValuesSourceType .NUMERIC , CoreValuesSourceType .DATE , CoreValuesSourceType .BOOLEAN ),
53+             new  MetricAggregatorSupplier () {
54+                 @ Override 
55+                 public  Aggregator  build (String  name ,
56+                                         ValuesSource  valuesSource ,
57+                                         DocValueFormat  formatter ,
58+                                         SearchContext  context ,
59+                                         Aggregator  parent ,
60+                                         List <PipelineAggregator > pipelineAggregators , Map <String , Object > metaData ) throws  IOException  {
61+                     return  new  AvgAggregator (name , (Numeric ) valuesSource , formatter , context , parent , pipelineAggregators , metaData );
62+                 }
63+             });
64+     }
65+ 
4666    @ Override 
4767    protected  Aggregator  createUnmapped (SearchContext  searchContext ,
4868                                            Aggregator  parent ,
@@ -58,11 +78,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
5878                                            boolean  collectsFromSingleBucket ,
5979                                            List <PipelineAggregator > pipelineAggregators ,
6080                                            Map <String , Object > metaData ) throws  IOException  {
81+         AggregatorSupplier  aggregatorSupplier  = queryShardContext .getValuesSourceRegistry ().getAggregator (config .valueSourceType (),
82+             AvgAggregationBuilder .NAME );
6183
62-         if  (valuesSource  instanceof  Numeric  == false ) {
63-             throw  new  AggregationExecutionException ("ValuesSource type "   +  valuesSource . toString () +  "is not supported for aggregation  "
64-                 this . name () );
84+         if  (aggregatorSupplier  instanceof  MetricAggregatorSupplier  == false ) {
85+             throw  new  AggregationExecutionException ("Registry miss-match - expected MetricAggregatorSupplier, found [ "  +
86+                 aggregatorSupplier . getClass (). toString () +  "]" );
6587        }
66-         return  new  AvgAggregator (name , (Numeric ) valuesSource , config .format (), searchContext , parent , pipelineAggregators , metaData );
88+         return  ((MetricAggregatorSupplier ) aggregatorSupplier ).build (name , valuesSource , config .format (), searchContext , parent ,
89+             pipelineAggregators , metaData );
6790    }
6891}
0 commit comments