forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregationContainer.cs
601 lines (416 loc) · 23.1 KB
/
AggregationContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Nest.Aggregations.Visitor;
namespace Nest
{
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
public class AggregationDictionary : IsADictionaryBase<string, IAggregationContainer>
{
public AggregationDictionary() : base() { }
public AggregationDictionary(IDictionary<string, IAggregationContainer> container) : base(container) { }
public AggregationDictionary(Dictionary<string, AggregationContainer> container)
: base(container.Select(kv => kv).ToDictionary(kv => kv.Key, kv => (IAggregationContainer)kv.Value))
{ }
public static implicit operator AggregationDictionary(Dictionary<string, IAggregationContainer> container) =>
new AggregationDictionary(container);
public static implicit operator AggregationDictionary(Dictionary<string, AggregationContainer> container) =>
new AggregationDictionary(container);
public static implicit operator AggregationDictionary(AggregationBase aggregator)
{
IAggregation b;
var combinator = aggregator as AggregationCombinator;
if (combinator != null)
{
var dict = new Dictionary<string, AggregationContainer>();
foreach (var agg in combinator.Aggregations)
{
b = agg;
if (b.Name.IsNullOrEmpty())
throw new ArgumentException($"{aggregator.GetType().Name} .Name is not set!");
dict.Add(b.Name, agg);
}
return dict;
}
b = aggregator;
if (b.Name.IsNullOrEmpty())
throw new ArgumentException($"{aggregator.GetType().Name} .Name is not set!");
return new Dictionary<string, AggregationContainer> { { b.Name, aggregator } };
}
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeJsonConverter<AggregationContainer>))]
public interface IAggregationContainer
{
[JsonProperty("meta")]
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
IDictionary<string, object> Meta { get; set; }
[JsonProperty("avg")]
IAverageAggregation Average { get; set; }
[JsonProperty("date_histogram")]
IDateHistogramAggregation DateHistogram { get; set; }
[JsonProperty("percentiles")]
IPercentilesAggregation Percentiles { get; set; }
[JsonProperty("date_range")]
IDateRangeAggregation DateRange { get; set; }
[JsonProperty("extended_stats")]
IExtendedStatsAggregation ExtendedStats { get; set; }
[JsonProperty("filter")]
IFilterAggregation Filter { get; set; }
[JsonProperty("filters")]
IFiltersAggregation Filters { get; set; }
[JsonProperty("geo_distance")]
IGeoDistanceAggregation GeoDistance { get; set; }
[JsonProperty("geohash_grid")]
IGeoHashGridAggregation GeoHash { get; set; }
[JsonProperty("geo_bounds")]
IGeoBoundsAggregation GeoBounds { get; set; }
[JsonProperty("histogram")]
IHistogramAggregation Histogram { get; set; }
[JsonProperty("global")]
IGlobalAggregation Global { get; set; }
[JsonProperty("ip_range")]
IIpRangeAggregation IpRange { get; set; }
[JsonProperty("max")]
IMaxAggregation Max { get; set; }
[JsonProperty("min")]
IMinAggregation Min { get; set; }
[JsonProperty("cardinality")]
ICardinalityAggregation Cardinality { get; set; }
[JsonProperty("missing")]
IMissingAggregation Missing { get; set; }
[JsonProperty("nested")]
INestedAggregation Nested { get; set; }
[JsonProperty("reverse_nested")]
IReverseNestedAggregation ReverseNested { get; set; }
[JsonProperty("range")]
IRangeAggregation Range { get; set; }
[JsonProperty("stats")]
IStatsAggregator Stats { get; set; }
[JsonProperty("sum")]
ISumAggregation Sum { get; set; }
[JsonProperty("terms")]
ITermsAggregation Terms { get; set; }
[JsonProperty("significant_terms")]
ISignificantTermsAggregation SignificantTerms { get; set; }
[JsonProperty("value_count")]
IValueCountAggregation ValueCount { get; set; }
[JsonProperty("percentile_ranks")]
IPercentileRanksAggregation PercentileRanks { get; set; }
[JsonProperty("top_hits")]
ITopHitsAggregation TopHits { get; set; }
[JsonProperty("children")]
IChildrenAggregation Children { get; set; }
[JsonProperty("scripted_metric")]
IScriptedMetricAggregation ScriptedMetric { get; set; }
[JsonProperty("avg_bucket")]
IAverageBucketAggregation AverageBucket { get; set; }
[JsonProperty("derivative")]
IDerivativeAggregation Derivative { get; set; }
[JsonProperty("max_bucket")]
IMaxBucketAggregation MaxBucket { get; set; }
[JsonProperty("min_bucket")]
IMinBucketAggregation MinBucket { get; set; }
[JsonProperty("sum_bucket")]
ISumBucketAggregation SumBucket { get; set; }
[JsonProperty("stats_bucket")]
IStatsBucketAggregation StatsBucket { get; set; }
[JsonProperty("extended_stats_bucket")]
IExtendedStatsBucketAggregation ExtendedStatsBucket { get; set; }
[JsonProperty("percentiles_bucket")]
IPercentilesBucketAggregation PercentilesBucket { get; set; }
[JsonProperty("moving_avg")]
IMovingAverageAggregation MovingAverage { get; set; }
[JsonProperty("cumulative_sum")]
ICumulativeSumAggregation CumulativeSum { get; set; }
[JsonProperty("serial_diff")]
ISerialDifferencingAggregation SerialDifferencing { get; set; }
[JsonProperty("bucket_script")]
IBucketScriptAggregation BucketScript { get; set; }
[JsonProperty("bucket_selector")]
IBucketSelectorAggregation BucketSelector { get; set; }
[JsonProperty("sampler")]
ISamplerAggregation Sampler { get; set; }
[JsonProperty("aggs")]
AggregationDictionary Aggregations { get; set; }
void Accept(IAggregationVisitor visitor);
}
public class AggregationContainer : IAggregationContainer
{
public IDictionary<string, object> Meta { get; set; }
public IAverageAggregation Average { get; set; }
public IValueCountAggregation ValueCount { get; set; }
public IMaxAggregation Max { get; set; }
public IMinAggregation Min { get; set; }
public IStatsAggregator Stats { get; set; }
public ISumAggregation Sum { get; set; }
public IExtendedStatsAggregation ExtendedStats { get; set; }
public IDateHistogramAggregation DateHistogram { get; set; }
public IPercentilesAggregation Percentiles { get; set; }
public IDateRangeAggregation DateRange { get; set; }
public IFilterAggregation Filter { get; set; }
public IFiltersAggregation Filters { get; set; }
public IGeoDistanceAggregation GeoDistance { get; set; }
public IGeoHashGridAggregation GeoHash { get; set; }
public IGeoBoundsAggregation GeoBounds { get; set; }
public IHistogramAggregation Histogram { get; set; }
public IGlobalAggregation Global { get; set; }
public IIpRangeAggregation IpRange { get; set; }
public ICardinalityAggregation Cardinality { get; set; }
public IMissingAggregation Missing { get; set; }
public INestedAggregation Nested { get; set; }
public IReverseNestedAggregation ReverseNested { get; set; }
public IRangeAggregation Range { get; set; }
public ITermsAggregation Terms { get; set; }
public ISignificantTermsAggregation SignificantTerms { get; set; }
public IPercentileRanksAggregation PercentileRanks { get; set; }
public ITopHitsAggregation TopHits { get; set; }
public IChildrenAggregation Children { get; set; }
public IScriptedMetricAggregation ScriptedMetric { get; set; }
public IAverageBucketAggregation AverageBucket { get; set; }
public IDerivativeAggregation Derivative { get; set; }
public IMaxBucketAggregation MaxBucket { get; set; }
public IMinBucketAggregation MinBucket { get; set; }
public ISumBucketAggregation SumBucket { get; set; }
public IStatsBucketAggregation StatsBucket { get; set; }
public IExtendedStatsBucketAggregation ExtendedStatsBucket { get; set; }
public IPercentilesBucketAggregation PercentilesBucket { get; set; }
public IMovingAverageAggregation MovingAverage { get; set; }
public ICumulativeSumAggregation CumulativeSum { get; set; }
public ISerialDifferencingAggregation SerialDifferencing { get; set; }
public IBucketScriptAggregation BucketScript { get; set; }
public IBucketSelectorAggregation BucketSelector { get; set; }
public ISamplerAggregation Sampler { get; set; }
public AggregationDictionary Aggregations { get; set; }
public static implicit operator AggregationContainer(AggregationBase aggregator)
{
if (aggregator == null) return null;
var container = new AggregationContainer();
aggregator.WrapInContainer(container);
var bucket = aggregator as BucketAggregationBase;
container.Aggregations = bucket?.Aggregations;
container.Meta = aggregator.Meta;
return container;
}
public void Accept(IAggregationVisitor visitor)
{
if (visitor.Scope == AggregationVisitorScope.Unknown) visitor.Scope = AggregationVisitorScope.Aggregation;
new AggregationWalker().Walk(this, visitor);
}
}
public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationContainerDescriptor<T>, IAggregationContainer>, IAggregationContainer
where T : class
{
IDictionary<string, object> IAggregationContainer.Meta { get; set; }
AggregationDictionary IAggregationContainer.Aggregations { get; set; }
IAverageAggregation IAggregationContainer.Average { get; set; }
IDateHistogramAggregation IAggregationContainer.DateHistogram { get; set; }
IPercentilesAggregation IAggregationContainer.Percentiles { get; set; }
IDateRangeAggregation IAggregationContainer.DateRange { get; set; }
IExtendedStatsAggregation IAggregationContainer.ExtendedStats { get; set; }
IFilterAggregation IAggregationContainer.Filter { get; set; }
IFiltersAggregation IAggregationContainer.Filters { get; set; }
IGeoDistanceAggregation IAggregationContainer.GeoDistance { get; set; }
IGeoHashGridAggregation IAggregationContainer.GeoHash { get; set; }
IGeoBoundsAggregation IAggregationContainer.GeoBounds { get; set; }
IHistogramAggregation IAggregationContainer.Histogram { get; set; }
IGlobalAggregation IAggregationContainer.Global { get; set; }
IIpRangeAggregation IAggregationContainer.IpRange { get; set; }
IMaxAggregation IAggregationContainer.Max { get; set; }
IMinAggregation IAggregationContainer.Min { get; set; }
ICardinalityAggregation IAggregationContainer.Cardinality { get; set; }
IMissingAggregation IAggregationContainer.Missing { get; set; }
INestedAggregation IAggregationContainer.Nested { get; set; }
IReverseNestedAggregation IAggregationContainer.ReverseNested { get; set; }
IRangeAggregation IAggregationContainer.Range { get; set; }
IStatsAggregator IAggregationContainer.Stats { get; set; }
ISumAggregation IAggregationContainer.Sum { get; set; }
IValueCountAggregation IAggregationContainer.ValueCount { get; set; }
ISignificantTermsAggregation IAggregationContainer.SignificantTerms { get; set; }
IPercentileRanksAggregation IAggregationContainer.PercentileRanks { get; set; }
ITermsAggregation IAggregationContainer.Terms { get; set; }
ITopHitsAggregation IAggregationContainer.TopHits { get; set; }
IChildrenAggregation IAggregationContainer.Children { get; set; }
IScriptedMetricAggregation IAggregationContainer.ScriptedMetric { get; set; }
IAverageBucketAggregation IAggregationContainer.AverageBucket { get; set; }
IDerivativeAggregation IAggregationContainer.Derivative { get; set; }
IMaxBucketAggregation IAggregationContainer.MaxBucket { get; set; }
IMinBucketAggregation IAggregationContainer.MinBucket { get; set; }
ISumBucketAggregation IAggregationContainer.SumBucket { get; set; }
IStatsBucketAggregation IAggregationContainer.StatsBucket { get; set; }
IExtendedStatsBucketAggregation IAggregationContainer.ExtendedStatsBucket { get; set; }
IPercentilesBucketAggregation IAggregationContainer.PercentilesBucket { get; set; }
IMovingAverageAggregation IAggregationContainer.MovingAverage { get; set; }
ICumulativeSumAggregation IAggregationContainer.CumulativeSum { get; set; }
ISerialDifferencingAggregation IAggregationContainer.SerialDifferencing { get; set; }
IBucketScriptAggregation IAggregationContainer.BucketScript { get; set; }
IBucketSelectorAggregation IAggregationContainer.BucketSelector { get; set; }
ISamplerAggregation IAggregationContainer.Sampler { get; set; }
public AggregationContainerDescriptor<T> Average(string name,
Func<AverageAggregationDescriptor<T>, IAverageAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Average = d);
public AggregationContainerDescriptor<T> DateHistogram(string name,
Func<DateHistogramAggregationDescriptor<T>, IDateHistogramAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.DateHistogram = d);
public AggregationContainerDescriptor<T> Percentiles(string name,
Func<PercentilesAggregationDescriptor<T>, IPercentilesAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Percentiles = d);
public AggregationContainerDescriptor<T> PercentileRanks(string name,
Func<PercentileRanksAggregationDescriptor<T>, IPercentileRanksAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.PercentileRanks = d);
public AggregationContainerDescriptor<T> DateRange(string name,
Func<DateRangeAggregationDescriptor<T>, IDateRangeAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.DateRange = d);
public AggregationContainerDescriptor<T> ExtendedStats(string name,
Func<ExtendedStatsAggregationDescriptor<T>, IExtendedStatsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.ExtendedStats = d);
public AggregationContainerDescriptor<T> Filter(string name,
Func<FilterAggregationDescriptor<T>, IFilterAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Filter = d);
public AggregationContainerDescriptor<T> Filters(string name,
Func<FiltersAggregationDescriptor<T>, IFiltersAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Filters = d);
public AggregationContainerDescriptor<T> GeoDistance(string name,
Func<GeoDistanceAggregationDescriptor<T>, IGeoDistanceAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.GeoDistance = d);
public AggregationContainerDescriptor<T> GeoHash(string name,
Func<GeoHashGridAggregationDescriptor<T>, IGeoHashGridAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.GeoHash = d);
public AggregationContainerDescriptor<T> GeoBounds(string name,
Func<GeoBoundsAggregationDescriptor<T>, IGeoBoundsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.GeoBounds = d);
public AggregationContainerDescriptor<T> Histogram(string name,
Func<HistogramAggregationDescriptor<T>, IHistogramAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Histogram = d);
public AggregationContainerDescriptor<T> Global(string name,
Func<GlobalAggregationDescriptor<T>, IGlobalAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Global = d);
public AggregationContainerDescriptor<T> IpRange(string name,
Func<IpRangeAggregationDescriptor<T>, IIpRangeAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.IpRange = d);
public AggregationContainerDescriptor<T> Max(string name,
Func<MaxAggregationDescriptor<T>, IMaxAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Max = d);
public AggregationContainerDescriptor<T> Min(string name,
Func<MinAggregationDescriptor<T>, IMinAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Min = d);
public AggregationContainerDescriptor<T> Cardinality(string name,
Func<CardinalityAggregationDescriptor<T>, ICardinalityAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Cardinality = d);
public AggregationContainerDescriptor<T> Missing(string name,
Func<MissingAggregationDescriptor<T>, IMissingAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Missing = d);
public AggregationContainerDescriptor<T> Nested(string name,
Func<NestedAggregationDescriptor<T>, INestedAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Nested = d);
public AggregationContainerDescriptor<T> ReverseNested(string name,
Func<ReverseNestedAggregationDescriptor<T>, IReverseNestedAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.ReverseNested = d);
public AggregationContainerDescriptor<T> Range(string name,
Func<RangeAggregationDescriptor<T>, IRangeAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Range = d);
public AggregationContainerDescriptor<T> Stats(string name,
Func<StatsAggregationDescriptor<T>, IStatsAggregator> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Stats = d);
public AggregationContainerDescriptor<T> Sum(string name,
Func<SumAggregationDescriptor<T>, ISumAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Sum = d);
public AggregationContainerDescriptor<T> Terms(string name,
Func<TermsAggregationDescriptor<T>, ITermsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Terms = d);
public AggregationContainerDescriptor<T> SignificantTerms(string name,
Func<SignificantTermsAggregationDescriptor<T>, ISignificantTermsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.SignificantTerms = d);
public AggregationContainerDescriptor<T> ValueCount(string name,
Func<ValueCountAggregationDescriptor<T>, IValueCountAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.ValueCount = d);
public AggregationContainerDescriptor<T> TopHits(string name,
Func<TopHitsAggregationDescriptor<T>, ITopHitsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.TopHits = d);
public AggregationContainerDescriptor<T> Children<TChild>(string name,
Func<ChildrenAggregationDescriptor<TChild>, IChildrenAggregation> selector) where TChild : class =>
_SetInnerAggregation(name, selector, (a, d) => a.Children = d);
public AggregationContainerDescriptor<T> ScriptedMetric(string name,
Func<ScriptedMetricAggregationDescriptor<T>, IScriptedMetricAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.ScriptedMetric = d);
public AggregationContainerDescriptor<T> AverageBucket(string name,
Func<AverageBucketAggregationDescriptor, IAverageBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.AverageBucket = d);
public AggregationContainerDescriptor<T> Derivative(string name,
Func<DerivativeAggregationDescriptor, IDerivativeAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Derivative = d);
public AggregationContainerDescriptor<T> MaxBucket(string name,
Func<MaxBucketAggregationDescriptor, IMaxBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.MaxBucket = d);
public AggregationContainerDescriptor<T> MinBucket(string name,
Func<MinBucketAggregationDescriptor, IMinBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.MinBucket = d);
public AggregationContainerDescriptor<T> SumBucket(string name,
Func<SumBucketAggregationDescriptor, ISumBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.SumBucket = d);
public AggregationContainerDescriptor<T> StatsBucket(string name,
Func<StatsBucketAggregationDescriptor, IStatsBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.StatsBucket = d);
public AggregationContainerDescriptor<T> ExtendedStatsBucket(string name,
Func<ExtendedStatsBucketAggregationDescriptor, IExtendedStatsBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.ExtendedStatsBucket = d);
public AggregationContainerDescriptor<T> PercentilesBucket(string name,
Func<PercentilesBucketAggregationDescriptor, IPercentilesBucketAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.PercentilesBucket = d);
public AggregationContainerDescriptor<T> MovingAverage(string name,
Func<MovingAverageAggregationDescriptor, IMovingAverageAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.MovingAverage = d);
public AggregationContainerDescriptor<T> CumulativeSum(string name,
Func<CumulativeSumAggregationDescriptor, ICumulativeSumAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.CumulativeSum = d);
public AggregationContainerDescriptor<T> SerialDifferencing(string name,
Func<SerialDifferencingAggregationDescriptor, ISerialDifferencingAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.SerialDifferencing = d);
public AggregationContainerDescriptor<T> BucketScript(string name,
Func<BucketScriptAggregationDescriptor, IBucketScriptAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.BucketScript = d);
public AggregationContainerDescriptor<T> BucketSelector(string name,
Func<BucketSelectorAggregationDescriptor, IBucketSelectorAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.BucketSelector = d);
public AggregationContainerDescriptor<T> Sampler(string name,
Func<SamplerAggregationDescriptor<T>, ISamplerAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Sampler = d);
/// <summary>
/// Fluent methods do not assign to properties on `this` directly but on IAggregationContainers inside `this.Aggregations[string, IContainer]
/// </summary>
private AggregationContainerDescriptor<T> _SetInnerAggregation<TAggregator, TAggregatorInterface>(
string key,
Func<TAggregator, TAggregatorInterface> selector
, Action<IAggregationContainer, TAggregatorInterface> assignToProperty
)
where TAggregator : IAggregation, TAggregatorInterface, new()
where TAggregatorInterface : IAggregation
{
var aggregator = selector(new TAggregator());
//create new isolated container for new aggregator and assign to the right property
var container = new AggregationContainer() { Meta = aggregator.Meta };
assignToProperty(container, aggregator);
//create aggregations dictionary on `this` if it does not exist already
IAggregationContainer self = this;
if (self.Aggregations == null) self.Aggregations = new Dictionary<string, IAggregationContainer>();
//if the aggregator is a bucket aggregator (meaning it contains nested aggregations);
var bucket = aggregator as IBucketAggregation;
if (bucket != null && bucket.Aggregations.HasAny())
{
//make sure we copy those aggregations to the isolated container's
//own .Aggregations container (the one that gets serialized to "aggs")
IAggregationContainer d = container;
d.Aggregations = bucket.Aggregations;
}
//assign the aggregations container under Aggregations ("aggs" in the json)
self.Aggregations[key] = container;
return this;
}
public void Accept(IAggregationVisitor visitor)
{
if (visitor.Scope == AggregationVisitorScope.Unknown) visitor.Scope = AggregationVisitorScope.Aggregation;
new AggregationWalker().Walk(this, visitor);
}
}
}