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 .ValuesSourceAggregatorFactory ;
3033import org .elasticsearch .search .aggregations .support .ValuesSourceConfig ;
34+ import org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
3135import org .elasticsearch .search .internal .SearchContext ;
3236
3337import java .io .IOException ;
@@ -39,22 +43,49 @@ public class MedianAbsoluteDeviationAggregatorFactory extends ValuesSourceAggreg
3943 private final double compression ;
4044
4145 MedianAbsoluteDeviationAggregatorFactory (String name ,
42- ValuesSourceConfig config ,
43- QueryShardContext queryShardContext ,
44- AggregatorFactory parent ,
45- AggregatorFactories .Builder subFactoriesBuilder ,
46- Map <String , Object > metaData ,
47- double compression ) throws IOException {
46+ ValuesSourceConfig config ,
47+ QueryShardContext queryShardContext ,
48+ AggregatorFactory parent ,
49+ AggregatorFactories .Builder subFactoriesBuilder ,
50+ Map <String , Object > metaData ,
51+ double compression ) throws IOException {
4852
4953 super (name , config , queryShardContext , parent , subFactoriesBuilder , metaData );
5054 this .compression = compression ;
5155 }
5256
57+ static void registerAggregators (ValuesSourceRegistry valuesSourceRegistry ) {
58+ valuesSourceRegistry .register (MedianAbsoluteDeviationAggregationBuilder .NAME ,
59+ CoreValuesSourceType .NUMERIC ,
60+ new MedianAbsoluteDeviationAggregatorSupplier () {
61+ @ Override
62+ public Aggregator build (String name ,
63+ ValuesSource valuesSource ,
64+ DocValueFormat format ,
65+ SearchContext context ,
66+ Aggregator parent ,
67+ List <PipelineAggregator > pipelineAggregators ,
68+ Map <String , Object > metaData ,
69+ double compression ) throws IOException {
70+ return new MedianAbsoluteDeviationAggregator (
71+ name ,
72+ context ,
73+ parent ,
74+ pipelineAggregators ,
75+ metaData ,
76+ (ValuesSource .Numeric ) valuesSource ,
77+ format ,
78+ compression
79+ );
80+ }
81+ });
82+ }
83+
5384 @ Override
5485 protected Aggregator createUnmapped (SearchContext searchContext ,
55- Aggregator parent ,
56- List <PipelineAggregator > pipelineAggregators ,
57- Map <String , Object > metaData ) throws IOException {
86+ Aggregator parent ,
87+ List <PipelineAggregator > pipelineAggregators ,
88+ Map <String , Object > metaData ) throws IOException {
5889
5990 return new MedianAbsoluteDeviationAggregator (
6091 name ,
@@ -75,20 +106,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
75106 boolean collectsFromSingleBucket ,
76107 List <PipelineAggregator > pipelineAggregators ,
77108 Map <String , Object > metaData ) throws IOException {
109+ AggregatorSupplier aggregatorSupplier = queryShardContext .getValuesSourceRegistry ().getAggregator (config .valueSourceType (),
110+ MedianAbsoluteDeviationAggregationBuilder .NAME );
78111
79- if (valuesSource instanceof ValuesSource . Numeric == false ) {
80- throw new AggregationExecutionException ("ValuesSource type " + valuesSource . toString () + "is not supported for aggregation " +
81- this . name () );
112+ if (aggregatorSupplier instanceof MedianAbsoluteDeviationAggregatorSupplier == false ) {
113+ throw new AggregationExecutionException ("Registry miss-match - expected MedianAbsoluteDeviationAggregatorSupplier, found [ " +
114+ aggregatorSupplier . getClass (). toString () + "]" );
82115 }
83- return new MedianAbsoluteDeviationAggregator (
84- name ,
85- searchContext ,
86- parent ,
87- pipelineAggregators ,
88- metaData ,
89- (ValuesSource .Numeric ) valuesSource ,
90- config .format (),
91- compression
92- );
116+ return ((MedianAbsoluteDeviationAggregatorSupplier ) aggregatorSupplier ).build (name , valuesSource , config .format (),
117+ searchContext , parent , pipelineAggregators , metaData , compression );
93118 }
94119}
0 commit comments