1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Reflection ;
4+ using Elasticsearch . Net ;
5+ using Newtonsoft . Json ;
6+
7+ namespace Nest
8+ {
9+ public class AggregationDescriptor < T >
10+ where T : class
11+ {
12+ internal readonly IDictionary < string , AggregationDescriptor < T > > _Aggregations =
13+ new Dictionary < string , AggregationDescriptor < T > > ( ) ;
14+
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 )
23+ {
24+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Average = d ) ;
25+ }
26+
27+ [ JsonProperty ( "date_histogram" ) ]
28+ internal DateHistogramAggregationDescriptor < T > _DateHistogram { get ; set ; }
29+ public AggregationDescriptor < T > DateHistogram ( string name ,
30+ Func < DateHistogramAggregationDescriptor < T > , DateHistogramAggregationDescriptor < T > > selector )
31+ {
32+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _DateHistogram = d ) ;
33+ }
34+
35+ [ JsonProperty ( "date_range" ) ]
36+ internal DateRangeAggregationDescriptor < T > _DateRange { get ; set ; }
37+ public AggregationDescriptor < T > DateRange ( string name ,
38+ Func < DateRangeAggregationDescriptor < T > , DateRangeAggregationDescriptor < T > > selector )
39+ {
40+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _DateRange = d ) ;
41+ }
42+
43+ [ JsonProperty ( "extended_stats" ) ]
44+ internal ExtendedStatsAggregationDescriptor < T > _ExtendedStats { get ; set ; }
45+ public AggregationDescriptor < T > ExtendedStats ( string name ,
46+ Func < ExtendedStatsAggregationDescriptor < T > , ExtendedStatsAggregationDescriptor < T > > selector )
47+ {
48+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _ExtendedStats = d ) ;
49+ }
50+
51+ [ JsonProperty ( "filter" ) ]
52+ internal FilterAggregationDescriptor < T > _Filter { get ; set ; }
53+ public AggregationDescriptor < T > Filter ( string name ,
54+ Func < FilterAggregationDescriptor < T > , FilterAggregationDescriptor < T > > selector )
55+ {
56+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Filter = d ) ;
57+ }
58+
59+ [ JsonProperty ( "geo_distance" ) ]
60+ internal GeoDistanceAggregationDescriptor < T > _GeoDistance { get ; set ; }
61+ public AggregationDescriptor < T > GeoDistance ( string name ,
62+ Func < GeoDistanceAggregationDescriptor < T > , GeoDistanceAggregationDescriptor < T > > selector )
63+ {
64+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _GeoDistance = d ) ;
65+ }
66+
67+ [ JsonProperty ( "geohash_grid" ) ]
68+ internal GeoHashAggregationDescriptor < T > _GeoHash { get ; set ; }
69+ public AggregationDescriptor < T > GeoHash ( string name ,
70+ Func < GeoHashAggregationDescriptor < T > , GeoHashAggregationDescriptor < T > > selector )
71+ {
72+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _GeoHash = d ) ;
73+ }
74+
75+ [ JsonProperty ( "histogram" ) ]
76+ internal HistogramAggregationDescriptor < T > _Histogram { get ; set ; }
77+ public AggregationDescriptor < T > Histogram ( string name ,
78+ Func < HistogramAggregationDescriptor < T > , HistogramAggregationDescriptor < T > > selector )
79+ {
80+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Histogram = d ) ;
81+ }
82+
83+ [ JsonProperty ( "global" ) ]
84+ internal GlobalAggregationDescriptor < T > _Global { get ; set ; }
85+ public AggregationDescriptor < T > Global ( string name ,
86+ Func < GlobalAggregationDescriptor < T > , GlobalAggregationDescriptor < T > > selector )
87+ {
88+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Global = d ) ;
89+ }
90+
91+ [ JsonProperty ( "ip_range" ) ]
92+ internal Ip4RangeAggregationDescriptor < T > _IpRange { get ; set ; }
93+ public AggregationDescriptor < T > IpRange ( string name ,
94+ Func < Ip4RangeAggregationDescriptor < T > , Ip4RangeAggregationDescriptor < T > > selector )
95+ {
96+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _IpRange = d ) ;
97+ }
98+
99+ [ JsonProperty ( "max" ) ]
100+ internal MaxAggregationDescriptor < T > _Max { get ; set ; }
101+ public AggregationDescriptor < T > Max ( string name , Func < MaxAggregationDescriptor < T > , MaxAggregationDescriptor < T > > selector )
102+ {
103+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Max = d ) ;
104+ }
105+
106+ [ JsonProperty ( "min" ) ]
107+ internal MinAggregationDescriptor < T > _Min { get ; set ; }
108+ public AggregationDescriptor < T > Min ( string name , Func < MinAggregationDescriptor < T > , MinAggregationDescriptor < T > > selector )
109+ {
110+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Min = d ) ;
111+ }
112+
113+ [ JsonProperty ( "missing" ) ]
114+ internal MissingAggregationDescriptor < T > _Missing { get ; set ; }
115+ public AggregationDescriptor < T > Missing ( string name , Func < MissingAggregationDescriptor < T > , MissingAggregationDescriptor < T > > selector )
116+ {
117+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Missing = d ) ;
118+ }
119+
120+ [ JsonProperty ( "nested" ) ]
121+ internal NestedAggregationDescriptor < T > _Nested { get ; set ; }
122+ public AggregationDescriptor < T > Nested ( string name , Func < NestedAggregationDescriptor < T > , NestedAggregationDescriptor < T > > selector )
123+ {
124+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Nested = d ) ;
125+ }
126+
127+ [ JsonProperty ( "range" ) ]
128+ internal RangeAggregationDescriptor < T > _Range { get ; set ; }
129+ public AggregationDescriptor < T > Range ( string name , Func < RangeAggregationDescriptor < T > , RangeAggregationDescriptor < T > > selector )
130+ {
131+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Range = d ) ;
132+ }
133+
134+ [ JsonProperty ( "stats" ) ]
135+ internal StatsAggregationDescriptor < T > _Stats { get ; set ; }
136+ public AggregationDescriptor < T > Stats ( string name , Func < StatsAggregationDescriptor < T > , StatsAggregationDescriptor < T > > selector )
137+ {
138+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Stats = d ) ;
139+ }
140+
141+ [ JsonProperty ( "sum" ) ]
142+ internal SumAggregationDescriptor < T > _Sum { get ; set ; }
143+ public AggregationDescriptor < T > Sum ( string name , Func < SumAggregationDescriptor < T > , SumAggregationDescriptor < T > > selector )
144+ {
145+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Sum = d ) ;
146+ }
147+
148+ [ JsonProperty ( "terms" ) ]
149+ internal TermsAggregationDescriptor < T > _Terms { get ; set ; }
150+ public AggregationDescriptor < T > Terms ( string name , Func < TermsAggregationDescriptor < T > , TermsAggregationDescriptor < T > > selector )
151+ {
152+ return _SetInnerAggregation ( name , selector , ( a , d ) => a . _Terms = d ) ;
153+ }
154+
155+ [ JsonProperty ( "value_count" ) ]
156+ internal ValueCountAggregationDescriptor < T > _ValueCount { get ; set ; }
157+ public AggregationDescriptor < T > ValueCount ( string name ,
158+ Func < ValueCountAggregationDescriptor < T > , ValueCountAggregationDescriptor < T > > selector )
159+ {
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 ;
180+ return this ;
181+
182+ }
183+
184+
185+ }
186+ }
0 commit comments