Skip to content

Commit 2795096

Browse files
committed
Aggregation metadata support
Closes #1729
1 parent 8bdcbb3 commit 2795096

File tree

5 files changed

+118
-12
lines changed

5 files changed

+118
-12
lines changed

Diff for: src/Nest/Aggregations/Aggregation.cs

+5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ public interface IAggregationResult
88
{
99
}
1010

11+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
1112
public interface IAggregation
1213
{
1314
string Name { get; set; }
15+
16+
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
17+
[JsonProperty("meta")]
18+
IDictionary<string, object> Meta { get; set; }
1419
}
1520

1621
public abstract class AggregationBase : IAggregation

Diff for: src/Nest/Aggregations/Bucket/BucketAggregationBase.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Nest
45
{
@@ -32,8 +33,13 @@ protected TBucketAggregation Assign(Action<TBucketAggregationInterface> assigner
3233

3334
string IAggregation.Name { get; set; }
3435

36+
IDictionary<string, object> IAggregation.Meta { get; set; }
37+
3538
public TBucketAggregation Aggregations(Func<AggregationContainerDescriptor<T>, IAggregationContainer> selector) =>
3639
Assign(a => a.Aggregations = selector?.Invoke(new AggregationContainerDescriptor<T>())?.Aggregations);
40+
41+
public TBucketAggregation Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
42+
Assign(a => a.Meta = selector?.Invoke(new FluentDictionary<string, object>()));
3743
}
3844

3945
}

Diff for: src/Nest/Aggregations/Metric/MetricAggregationBase.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34
using Newtonsoft.Json;
45

@@ -45,6 +46,8 @@ public abstract class MetricAggregationDescriptorBase<TMetricAggregation, TMetri
4546

4647
string IAggregation.Name { get; set; }
4748

49+
IDictionary<string, object> IAggregation.Meta { get; set; }
50+
4851
public TMetricAggregation Field(string field) => Assign(a => a.Field = field);
4952

5053
public TMetricAggregation Field(Expression<Func<T, object>> field) => Assign(a => a.Field = field);
@@ -55,5 +58,8 @@ public virtual TMetricAggregation Script(Func<ScriptDescriptor, IScript> scriptS
5558
Assign(a => a.Script = scriptSelector?.Invoke(new ScriptDescriptor()));
5659

5760
public TMetricAggregation Missing(double missing) => Assign(a => a.Missing = missing);
61+
62+
public TMetricAggregation Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
63+
Assign(a => a.Meta = selector?.Invoke(new FluentDictionary<string, object>()));
5864
}
5965
}

Diff for: src/Nest/Aggregations/Pipeline/PipelineAggregationBase.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Newtonsoft.Json;
34

45
namespace Nest
@@ -42,10 +43,15 @@ public abstract class PipelineAggregationDescriptorBase<TPipelineAggregation, TP
4243

4344
string IAggregation.Name { get; set; }
4445

46+
IDictionary<string, object> IAggregation.Meta { get; set; }
47+
4548
public TPipelineAggregation Format(string format) => Assign(a => a.Format = format);
4649

4750
public TPipelineAggregation GapPolicy(GapPolicy gapPolicy) => Assign(a => a.GapPolicy = gapPolicy);
4851

4952
public TPipelineAggregation BucketsPath(TBucketsPath bucketsPath) => Assign(a => a.BucketsPath = bucketsPath);
53+
54+
public TPipelineAggregation Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
55+
Assign(a => a.Meta = selector?.Invoke(new FluentDictionary<string, object>()));
5056
}
5157
}

Diff for: src/Tests/Aggregations/WritingAggregations.doc.cs

+95-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Tests.Framework;
44
using Tests.Framework.MockData;
55
using static Nest.Infer;
6+
using System.Collections.Generic;
67

78
namespace Tests.Aggregations
89
{
@@ -69,23 +70,105 @@ public class Usage : UsageTestBase<ISearchRequest, SearchDescriptor<Project>, Se
6970
};
7071
}
7172

72-
public class AggregationDslUsage : Usage
73+
public class MetaUsage : Usage
7374
{
74-
/**
75-
* For this reason the OIS syntax can be shortened dramatically by using `*Agg` related family,
76-
* These allow you to forego introducing intermediary Dictionaries to represent the aggregation DSL.
77-
* It also allows you to combine multiple aggregations using bitwise AND (`&&`) operator.
78-
*
79-
* Compare the following example with the previous vanilla OIS syntax
80-
*/
75+
protected override object ExpectJson => new
76+
{
77+
aggs = new
78+
{
79+
my_terms_agg = new
80+
{
81+
terms = new
82+
{
83+
field = "name",
84+
meta = new
85+
{
86+
foo = "bar",
87+
count = 1
88+
}
89+
}
90+
},
91+
my_avg_agg = new
92+
{
93+
avg = new
94+
{
95+
field = "numberOfCommits",
96+
meta = new
97+
{
98+
foo = "bar",
99+
count = 1
100+
}
101+
}
102+
},
103+
my_derivative_agg = new
104+
{
105+
derivative = new
106+
{
107+
buckets_path = "my_avg_agg",
108+
meta = new
109+
{
110+
foo = "bar",
111+
count = 1
112+
}
113+
}
114+
}
115+
}
116+
};
117+
118+
119+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
120+
.Aggregations(aggs => aggs
121+
.Terms("my_terms_agg", t => t
122+
.Field(p => p.Name)
123+
.Meta(meta => meta
124+
.Add("foo", "bar")
125+
.Add("count", 1)
126+
)
127+
)
128+
.Average("my_avg_agg", avg => avg
129+
.Field(p => p.NumberOfCommits)
130+
.Meta(meta => meta
131+
.Add("foo", "bar")
132+
.Add("count", 1)
133+
)
134+
)
135+
.Derivative("my_derivative_agg", dv => dv
136+
.BucketsPath("my_avg_agg")
137+
.Meta(meta => meta
138+
.Add("foo", "bar")
139+
.Add("count", 1)
140+
)
141+
)
142+
);
143+
144+
81145
protected override SearchRequest<Project> Initializer =>
82146
new SearchRequest<Project>
83147
{
84-
Aggregations = new ChildrenAggregation("name_of_child_agg", typeof(CommitActivity))
148+
Aggregations = new TermsAggregation("my_terms_agg")
85149
{
86-
Aggregations =
87-
new AverageAggregation("average_per_child", Field<CommitActivity>(p=>p.ConfidenceFactor))
88-
&& new MaxAggregation("max_per_child", Field<CommitActivity>(p=>p.ConfidenceFactor))
150+
Field = "name",
151+
Meta = new Dictionary<string, object>
152+
{
153+
{ "foo", "bar" },
154+
{ "count", 1 }
155+
}
156+
}
157+
&& new AverageAggregation("my_avg_agg", "numberOfCommits")
158+
{
159+
Meta = new Dictionary<string, object>
160+
{
161+
{ "foo", "bar" },
162+
{ "count", 1 }
163+
}
164+
}
165+
&& new DerivativeAggregation("my_derivative_agg", "my_avg_agg")
166+
{
167+
Meta = new Dictionary<string, object>
168+
{
169+
{ "foo", "bar" },
170+
{ "count", 1 }
171+
}
89172
}
90173
};
91174
}

0 commit comments

Comments
 (0)