11using System ;
22using System . Collections . Generic ;
33using System . Reflection ;
4+ using Elasticsearch . Net ;
45using Newtonsoft . Json ;
56
67namespace Nest
78{
89 public class AggregationDescriptor < T >
910 where T : class
1011 {
11- private readonly IDictionary < string , IAggregationDescriptor > _aggregations =
12- new Dictionary < string , IAggregationDescriptor > ( ) ;
12+ internal readonly IDictionary < string , AggregationDescriptor < T > > _Aggregations =
13+ new Dictionary < string , AggregationDescriptor < T > > ( ) ;
1314
14- public AverageAggregationDescriptor < T > _Average { get ; set ; }
15- public AggregationDescriptor < T > Average ( Func < AverageAggregationDescriptor < T > , AverageAggregationDescriptor < T > > selector )
15+ [ JsonProperty ( "aggs" , Order = 100 ) ]
16+ [ JsonConverter ( typeof ( DictionaryKeysAreNotPropertyNamesJsonConverter ) ) ]
17+ internal IDictionary < string , AggregationDescriptor < T > > _NestedAggregations ;
18+
19+
20+ [ JsonProperty ( "avg" ) ]
21+ internal AverageAggregationDescriptor < T > _Average { get ; set ; }
22+ public AggregationDescriptor < T > Average ( string name , Func < AverageAggregationDescriptor < T > , AverageAggregationDescriptor < T > > selector )
1623 {
17- this . _Average = selector ( new AverageAggregationDescriptor < T > ( ) ) ;
18- return this ;
24+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Average = d ) ;
1925 }
2026
2127 [ JsonProperty ( "date_histogram" ) ]
22- public DateHistogramAggregationDescriptor < T > _DateHistogram { get ; set ; }
23- public AggregationDescriptor < T > DateHistogram (
28+ internal DateHistogramAggregationDescriptor < T > _DateHistogram { get ; set ; }
29+ public AggregationDescriptor < T > DateHistogram ( string name ,
2430 Func < DateHistogramAggregationDescriptor < T > , DateHistogramAggregationDescriptor < T > > selector )
2531 {
26- this . _DateHistogram = selector ( new DateHistogramAggregationDescriptor < T > ( ) ) ;
27- return this ;
32+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _DateHistogram = d ) ;
2833 }
2934
3035 [ JsonProperty ( "date_range" ) ]
31- public DateRangeAggregationDescriptor < T > _DateRange { get ; set ; }
32- public AggregationDescriptor < T > DateRange (
36+ internal DateRangeAggregationDescriptor < T > _DateRange { get ; set ; }
37+ public AggregationDescriptor < T > DateRange ( string name ,
3338 Func < DateRangeAggregationDescriptor < T > , DateRangeAggregationDescriptor < T > > selector )
3439 {
35- this . _DateRange = selector ( new DateRangeAggregationDescriptor < T > ( ) ) ;
36- return this ;
40+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _DateRange = d ) ;
3741 }
3842
3943 [ JsonProperty ( "extended_stats" ) ]
40- public ExtendedStatsAggregationDescriptor < T > _ExtendedStats { get ; set ; }
41- public AggregationDescriptor < T > ExtendedStats (
44+ internal ExtendedStatsAggregationDescriptor < T > _ExtendedStats { get ; set ; }
45+ public AggregationDescriptor < T > ExtendedStats ( string name ,
4246 Func < ExtendedStatsAggregationDescriptor < T > , ExtendedStatsAggregationDescriptor < T > > selector )
4347 {
44- this . _ExtendedStats = selector ( new ExtendedStatsAggregationDescriptor < T > ( ) ) ;
45- return this ;
48+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _ExtendedStats = d ) ;
4649 }
4750
4851 [ JsonProperty ( "filter" ) ]
49- public FilterAggregationDescriptor < T > _Filter { get ; set ; }
50- public AggregationDescriptor < T > Filter (
52+ internal FilterAggregationDescriptor < T > _Filter { get ; set ; }
53+ public AggregationDescriptor < T > Filter ( string name ,
5154 Func < FilterAggregationDescriptor < T > , FilterAggregationDescriptor < T > > selector )
5255 {
53- this . _Filter = selector ( new FilterAggregationDescriptor < T > ( ) ) ;
54- return this ;
56+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Filter = d ) ;
5557 }
5658
5759 [ JsonProperty ( "geo_distance" ) ]
58- public GeoDistanceAggregationDescriptor < T > _GeoDistance { get ; set ; }
59- public AggregationDescriptor < T > GeoDistance (
60+ internal GeoDistanceAggregationDescriptor < T > _GeoDistance { get ; set ; }
61+ public AggregationDescriptor < T > GeoDistance ( string name ,
6062 Func < GeoDistanceAggregationDescriptor < T > , GeoDistanceAggregationDescriptor < T > > selector )
6163 {
62- this . _GeoDistance = selector ( new GeoDistanceAggregationDescriptor < T > ( ) ) ;
63- return this ;
64+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _GeoDistance = d ) ;
6465 }
6566
6667 [ JsonProperty ( "geo_hash" ) ]
67- public GeoHashAggregationDescriptor < T > _GeoHash { get ; set ; }
68- public AggregationDescriptor < T > GeoHash (
68+ internal GeoHashAggregationDescriptor < T > _GeoHash { get ; set ; }
69+ public AggregationDescriptor < T > GeoHash ( string name ,
6970 Func < GeoHashAggregationDescriptor < T > , GeoHashAggregationDescriptor < T > > selector )
7071 {
71- this . _GeoHash = selector ( new GeoHashAggregationDescriptor < T > ( ) ) ;
72- return this ;
72+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _GeoHash = d ) ;
7373 }
7474
7575 [ JsonProperty ( "histogram" ) ]
76- public HistogramAggregationDescriptor < T > _Histogram { get ; set ; }
77- public AggregationDescriptor < T > Histogram (
76+ internal HistogramAggregationDescriptor < T > _Histogram { get ; set ; }
77+ public AggregationDescriptor < T > Histogram ( string name ,
7878 Func < HistogramAggregationDescriptor < T > , HistogramAggregationDescriptor < T > > selector )
7979 {
80- this . _Histogram = selector ( new HistogramAggregationDescriptor < T > ( ) ) ;
81- return this ;
80+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Histogram = d ) ;
8281 }
8382
8483 [ JsonProperty ( "global" ) ]
85- public GlobalAggregationDescriptor < T > _Global { get ; set ; }
86- public AggregationDescriptor < T > Global (
84+ internal GlobalAggregationDescriptor < T > _Global { get ; set ; }
85+ public AggregationDescriptor < T > Global ( string name ,
8786 Func < GlobalAggregationDescriptor < T > , GlobalAggregationDescriptor < T > > selector )
8887 {
89- this . _Global = selector ( new GlobalAggregationDescriptor < T > ( ) ) ;
90- return this ;
88+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Global = d ) ;
9189 }
9290
93- [ JsonProperty ( "ip4_range " ) ]
94- public Ip4RangeAggregationDescriptor < T > _Ip4Range { get ; set ; }
95- public AggregationDescriptor < T > Ip4Range (
96- Func < Ip4RangeAggregationDescriptor < T > , Ip4RangeAggregationDescriptor < T > > selector )
91+ [ JsonProperty ( "ip_range " ) ]
92+ internal IpRangeAggregationDescriptor < T > _IpRange { get ; set ; }
93+ public AggregationDescriptor < T > IpRange ( string name ,
94+ Func < IpRangeAggregationDescriptor < T > , IpRangeAggregationDescriptor < T > > selector )
9795 {
98- this . _Ip4Range = selector ( new Ip4RangeAggregationDescriptor < T > ( ) ) ;
99- return this ;
96+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _IpRange = d ) ;
10097 }
10198
10299 [ JsonProperty ( "max" ) ]
103- public MaxAggregationDescriptor < T > _Max { get ; set ; }
104- public AggregationDescriptor < T > Max ( Func < MaxAggregationDescriptor < T > , MaxAggregationDescriptor < T > > selector )
100+ internal MaxAggregationDescriptor < T > _Max { get ; set ; }
101+ public AggregationDescriptor < T > Max ( string name , Func < MaxAggregationDescriptor < T > , MaxAggregationDescriptor < T > > selector )
105102 {
106- this . _Max = selector ( new MaxAggregationDescriptor < T > ( ) ) ;
107- return this ;
103+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Max = d ) ;
108104 }
109105
110106 [ JsonProperty ( "min" ) ]
111- public MinAggregationDescriptor < T > _Min { get ; set ; }
112- public AggregationDescriptor < T > Min ( Func < MinAggregationDescriptor < T > , MinAggregationDescriptor < T > > selector )
107+ internal MinAggregationDescriptor < T > _Min { get ; set ; }
108+ public AggregationDescriptor < T > Min ( string name , Func < MinAggregationDescriptor < T > , MinAggregationDescriptor < T > > selector )
113109 {
114- this . _Min = selector ( new MinAggregationDescriptor < T > ( ) ) ;
115- return this ;
110+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Min = d ) ;
116111 }
117112
118113 [ JsonProperty ( "missing" ) ]
119- public MissingAggregationDescriptor < T > _Missing { get ; set ; }
120- public AggregationDescriptor < T > Missing ( Func < MissingAggregationDescriptor < T > , MissingAggregationDescriptor < T > > selector )
114+ internal MissingAggregationDescriptor < T > _Missing { get ; set ; }
115+ public AggregationDescriptor < T > Missing ( string name , Func < MissingAggregationDescriptor < T > , MissingAggregationDescriptor < T > > selector )
121116 {
122- this . _Missing = selector ( new MissingAggregationDescriptor < T > ( ) ) ;
123- return this ;
117+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Missing = d ) ;
124118 }
125119
126120 [ JsonProperty ( "nested" ) ]
127- public NestedAggregationDescriptor < T > _Nested { get ; set ; }
128- public AggregationDescriptor < T > Nested ( Func < NestedAggregationDescriptor < T > , NestedAggregationDescriptor < T > > selector )
121+ internal NestedAggregationDescriptor < T > _Nested { get ; set ; }
122+ public AggregationDescriptor < T > Nested ( string name , Func < NestedAggregationDescriptor < T > , NestedAggregationDescriptor < T > > selector )
129123 {
130- this . _Nested = selector ( new NestedAggregationDescriptor < T > ( ) ) ;
131- return this ;
124+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Nested = d ) ;
132125 }
133126
134127 [ JsonProperty ( "range" ) ]
135- public RangeAggregationDescriptor < T > _Range { get ; set ; }
136- public AggregationDescriptor < T > Range ( Func < RangeAggregationDescriptor < T > , RangeAggregationDescriptor < T > > selector )
128+ internal RangeAggregationDescriptor < T > _Range { get ; set ; }
129+ public AggregationDescriptor < T > Range ( string name , Func < RangeAggregationDescriptor < T > , RangeAggregationDescriptor < T > > selector )
137130 {
138- this . _Range = selector ( new RangeAggregationDescriptor < T > ( ) ) ;
139- return this ;
131+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Range = d ) ;
140132 }
141133
142134 [ JsonProperty ( "stats" ) ]
143- public StatsAggregationDescriptor < T > _Stats { get ; set ; }
144- public AggregationDescriptor < T > Stats ( Func < StatsAggregationDescriptor < T > , StatsAggregationDescriptor < T > > selector )
135+ internal StatsAggregationDescriptor < T > _Stats { get ; set ; }
136+ public AggregationDescriptor < T > Stats ( string name , Func < StatsAggregationDescriptor < T > , StatsAggregationDescriptor < T > > selector )
145137 {
146- this . _Stats = selector ( new StatsAggregationDescriptor < T > ( ) ) ;
147- return this ;
138+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Stats = d ) ;
148139 }
149140
150141 [ JsonProperty ( "sum" ) ]
151- public SumAggregationDescriptor < T > _Sum { get ; set ; }
152- public AggregationDescriptor < T > Sum ( Func < SumAggregationDescriptor < T > , SumAggregationDescriptor < T > > selector )
142+ internal SumAggregationDescriptor < T > _Sum { get ; set ; }
143+ public AggregationDescriptor < T > Sum ( string name , Func < SumAggregationDescriptor < T > , SumAggregationDescriptor < T > > selector )
153144 {
154- this . _Sum = selector ( new SumAggregationDescriptor < T > ( ) ) ;
155- return this ;
145+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Sum = d ) ;
156146 }
157147
158148 [ JsonProperty ( "terms" ) ]
159- public TermsAggregationDescriptor < T > _Terms { get ; set ; }
160- public AggregationDescriptor < T > Terms ( Func < TermsAggregationDescriptor < T > , TermsAggregationDescriptor < T > > selector )
149+ internal TermsAggregationDescriptor < T > _Terms { get ; set ; }
150+ public AggregationDescriptor < T > Terms ( string name , Func < TermsAggregationDescriptor < T > , TermsAggregationDescriptor < T > > selector )
161151 {
162- this . _Terms = selector ( new TermsAggregationDescriptor < T > ( ) ) ;
163- return this ;
152+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Terms = d ) ;
164153 }
165154
166155 [ JsonProperty ( "value_count" ) ]
167- public ValueCountAggregationDescriptor < T > _ValueCount { get ; set ; }
168- public AggregationDescriptor < T > ValueCount (
156+ internal ValueCountAggregationDescriptor < T > _ValueCount { get ; set ; }
157+ public AggregationDescriptor < T > ValueCount ( string name ,
169158 Func < ValueCountAggregationDescriptor < T > , ValueCountAggregationDescriptor < T > > selector )
170159 {
171- this . _ValueCount = selector ( new ValueCountAggregationDescriptor < T > ( ) ) ;
160+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _ValueCount = d ) ;
161+ }
162+
163+ private AggregationDescriptor < T > _SetInnerAggregation < TAggregation > (
164+ string key ,
165+ Func < TAggregation , TAggregation > selector
166+ , Action < AggregationDescriptor < T > , TAggregation > setter
167+ )
168+ where TAggregation : IAggregationDescriptor , new ( )
169+
170+ {
171+ var innerDescriptor = selector ( new TAggregation ( ) ) ;
172+ var descriptor = new AggregationDescriptor < T > ( ) ;
173+ setter ( descriptor , innerDescriptor ) ;
174+ var bucket = innerDescriptor as IBucketAggregationDescriptor < T > ;
175+ if ( bucket != null && bucket . NestedAggregations . HasAny ( ) )
176+ {
177+ descriptor . _NestedAggregations = bucket . NestedAggregations ;
178+ }
179+ this . _Aggregations [ key ] = descriptor ;
172180 return this ;
181+
173182 }
174183
184+
175185 }
176186}
0 commit comments