Skip to content

Commit c42cc53

Browse files
committed
Merge pull request #491 from Mpdreamz/feature/aggregations
Aggregation support finally landed in NEST
2 parents 298b4ed + 13c91bd commit c42cc53

File tree

63 files changed

+2751
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2751
-3
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Nest.Resolvers;
7+
using Newtonsoft.Json;
8+
9+
namespace Nest
10+
{
11+
public class AverageAggregationDescriptor<T> : MetricAggregationBaseDescriptor<AverageAggregationDescriptor<T>, T>
12+
where T : class
13+
{
14+
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Newtonsoft.Json;
5+
6+
namespace Nest
7+
{
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>
18+
where TBucketAggregation : BucketAggregationBaseDescriptor<TBucketAggregation, T>
19+
where T : class
20+
{
21+
IDictionary<string, AggregationDescriptor<T>> IBucketAggregationDescriptor<T>.NestedAggregations { get; set; }
22+
23+
public TBucketAggregation Aggregations(Func<AggregationDescriptor<T>, AggregationDescriptor<T>> selector)
24+
{
25+
var aggs = selector(new AggregationDescriptor<T>());
26+
if (aggs == null) return (TBucketAggregation)this;
27+
((IBucketAggregationDescriptor<T>)this).NestedAggregations = aggs._Aggregations;
28+
return (TBucketAggregation)this;
29+
}
30+
}
31+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using Elasticsearch.Net;
5+
using Nest.Resolvers;
6+
using Nest.Resolvers.Converters;
7+
using Newtonsoft.Json;
8+
9+
namespace Nest
10+
{
11+
public class DateHistogramAggregationDescriptor<T> : BucketAggregationBaseDescriptor<DateHistogramAggregationDescriptor<T>, T>
12+
where T : class
13+
{
14+
[JsonProperty("field")]
15+
internal PropertyPathMarker _Field { get; set; }
16+
17+
public DateHistogramAggregationDescriptor()
18+
{
19+
this.Format("yyyy-MM-dd");
20+
}
21+
22+
23+
public DateHistogramAggregationDescriptor<T> Field(string field)
24+
{
25+
this._Field = field;
26+
return this;
27+
}
28+
29+
public DateHistogramAggregationDescriptor<T> Field(Expression<Func<T, object>> field)
30+
{
31+
this._Field = field;
32+
return this;
33+
}
34+
35+
[JsonProperty("script")]
36+
internal string _Script { get; set; }
37+
38+
public DateHistogramAggregationDescriptor<T> Script(string script)
39+
{
40+
this._Script = script;
41+
return this;
42+
}
43+
44+
[JsonProperty("params")]
45+
internal FluentDictionary<string, object> _Params { get; set; }
46+
47+
public DateHistogramAggregationDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramSelector)
48+
{
49+
this._Params = paramSelector(new FluentDictionary<string, object>());
50+
return this;
51+
}
52+
53+
[JsonProperty("interval")]
54+
internal string _Interval { get; set; }
55+
56+
public DateHistogramAggregationDescriptor<T> Interval(string interval)
57+
{
58+
this._Interval = interval;
59+
return this;
60+
}
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+
73+
[JsonProperty("min_doc_count")]
74+
internal int? _MinimumDocumentCount { get; set; }
75+
76+
public DateHistogramAggregationDescriptor<T> MinimumDocumentCount(int minimumDocumentCount)
77+
{
78+
this._MinimumDocumentCount = minimumDocumentCount;
79+
return this;
80+
}
81+
82+
[JsonProperty("pre_zone")]
83+
internal string _PreZone { get; set; }
84+
85+
public DateHistogramAggregationDescriptor<T> PreZone(string preZone)
86+
{
87+
this._PreZone = preZone;
88+
return this;
89+
}
90+
91+
[JsonProperty("post_zone")]
92+
internal string _PostZone { get; set; }
93+
94+
public DateHistogramAggregationDescriptor<T> PostZone(string postZone)
95+
{
96+
this._PostZone = postZone;
97+
return this;
98+
}
99+
100+
[JsonProperty("time_zone")]
101+
internal string _TimeZone { get; set; }
102+
103+
public DateHistogramAggregationDescriptor<T> TimeZone(string timeZone)
104+
{
105+
this._TimeZone = timeZone;
106+
return this;
107+
}
108+
[JsonProperty("pre_zone_adjust_large_interval")]
109+
internal bool? _PreZoneAdjustLargeInterval { get; set; }
110+
111+
public DateHistogramAggregationDescriptor<T> PreZoneAdjustLargeInterval(bool adjustLargeInterval = true)
112+
{
113+
this._PreZoneAdjustLargeInterval = adjustLargeInterval;
114+
return this;
115+
}
116+
[JsonProperty("factor")]
117+
internal int? _Factor { get; set; }
118+
119+
public DateHistogramAggregationDescriptor<T> Interval(int factor)
120+
{
121+
this._Factor = factor;
122+
return this;
123+
}
124+
125+
[JsonProperty("pre_offset")]
126+
internal string _PreOffset { get; set; }
127+
128+
public DateHistogramAggregationDescriptor<T> PreOffset(string preOffset)
129+
{
130+
this._PreOffset = preOffset;
131+
return this;
132+
}
133+
[JsonProperty("post_offset")]
134+
internal string _PostOffset { get; set; }
135+
136+
public DateHistogramAggregationDescriptor<T> PostOffset(string postOffset)
137+
{
138+
this._PostOffset = postOffset;
139+
return this;
140+
}
141+
142+
[JsonProperty("order")]
143+
internal IDictionary<string, string> _Order { get; set; }
144+
145+
public DateHistogramAggregationDescriptor<T> OrderAscending(string key)
146+
{
147+
this._Order = new Dictionary<string, string> { {key, "asc"}};
148+
return this;
149+
}
150+
151+
public DateHistogramAggregationDescriptor<T> OrderDescending(string key)
152+
{
153+
this._Order = new Dictionary<string, string> { {key, "asc"}};
154+
return this;
155+
}
156+
157+
}
158+
}

0 commit comments

Comments
 (0)