Skip to content

Commit d26f68d

Browse files
committed
most of the aggregations responses are mapped and the API is shaping up quite nicely
1 parent 90c89a5 commit d26f68d

21 files changed

+1104
-94
lines changed
Lines changed: 89 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,186 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
4+
using Elasticsearch.Net;
45
using Newtonsoft.Json;
56

67
namespace 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
}

src/Nest/DSL/Aggregations/BucketAggregationBaseDescriptor.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@
55

66
namespace Nest
77
{
8-
public abstract class BucketAggregationBaseDescriptor<TBucketAggregation, T>: IAggregationDescriptor
8+
9+
public interface IBucketAggregationDescriptor<T>
10+
where T : class
11+
{
12+
13+
IDictionary<string, AggregationDescriptor<T>> NestedAggregations { get; set; }
14+
}
15+
16+
public abstract class BucketAggregationBaseDescriptor<TBucketAggregation, T>
17+
: IAggregationDescriptor, IBucketAggregationDescriptor<T>
918
where TBucketAggregation : BucketAggregationBaseDescriptor<TBucketAggregation, T>
1019
where T : class
1120
{
12-
[JsonProperty("aggs")]
13-
internal AggregationDescriptor<T> _Aggregations;
21+
IDictionary<string, AggregationDescriptor<T>> IBucketAggregationDescriptor<T>.NestedAggregations { get; set; }
1422

1523
public TBucketAggregation Aggregations(Func<AggregationDescriptor<T>, AggregationDescriptor<T>> selector)
16-
{
17-
this._Aggregations = selector(new AggregationDescriptor<T>());
24+
{
25+
var aggs = selector(new AggregationDescriptor<T>());
26+
if (aggs == null) return (TBucketAggregation)this;
27+
((IBucketAggregationDescriptor<T>)this).NestedAggregations = aggs._Aggregations;
1828
return (TBucketAggregation)this;
1929
}
2030
}

src/Nest/DSL/Aggregations/DateHistogramAggregationDescriptor.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
using System.Linq.Expressions;
44
using Elasticsearch.Net;
55
using Nest.Resolvers;
6+
using Nest.Resolvers.Converters;
67
using Newtonsoft.Json;
78

89
namespace Nest
910
{
10-
public class DateHistogramAggregationDescriptor<T> : MetricAggregationBaseDescriptor<DateHistogramAggregationDescriptor<T>, T>
11+
public class DateHistogramAggregationDescriptor<T> : BucketAggregationBaseDescriptor<DateHistogramAggregationDescriptor<T>, T>
1112
where T : class
1213
{
1314
[JsonProperty("field")]
1415
internal PropertyPathMarker _Field { get; set; }
15-
16+
17+
public DateHistogramAggregationDescriptor()
18+
{
19+
this.Format("yyyy-MM-dd");
20+
}
21+
22+
1623
public DateHistogramAggregationDescriptor<T> Field(string field)
1724
{
1825
this._Field = field;
@@ -51,7 +58,18 @@ public DateHistogramAggregationDescriptor<T> Interval(string interval)
5158
this._Interval = interval;
5259
return this;
5360
}
54-
61+
62+
[JsonProperty("format")]
63+
internal string _Format { get; set; }
64+
65+
public DateHistogramAggregationDescriptor<T> Format(string format)
66+
{
67+
if (format.IsNullOrEmpty())
68+
return this;
69+
this._Format = format;
70+
return this;
71+
}
72+
5573
[JsonProperty("min_doc_count")]
5674
internal int? _MinimumDocumentCount { get; set; }
5775

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
using System;
2+
using Nest.Resolvers.Converters;
23
using Newtonsoft.Json;
34

45
namespace Nest
56
{
6-
public class FilterAggregationDescriptor<T> : BucketAggregationBaseDescriptor<FilterAggregationDescriptor<T>, T>
7+
[JsonConverter(typeof(CustomJsonConverter))]
8+
public class FilterAggregationDescriptor<T>
9+
: BucketAggregationBaseDescriptor<FilterAggregationDescriptor<T>, T>
10+
, ICustomJson
711
where T : class
812
{
9-
[JsonProperty("filter")]
1013
internal BaseFilter _Filter { get; set; }
1114

1215
public FilterAggregationDescriptor<T> Filter(Func<FilterDescriptor<T>, BaseFilter> selector)
1316
{
1417
this._Filter = selector(new FilterDescriptor<T>());
1518
return this;
1619
}
20+
21+
22+
object ICustomJson.GetCustomJson()
23+
{
24+
return _Filter;
25+
}
1726
}
1827
}

0 commit comments

Comments
 (0)