@@ -98,13 +98,14 @@ new SearchRequest<Project>
9898{
9999 Aggregations = new AggregationDictionary
100100 {
101- { "name_of_child_agg", new ChildrenAggregation("name_of_child_agg", typeof(CommitActivity))
101+ {
102+ "name_of_child_agg", new ChildrenAggregation("name_of_child_agg", typeof(CommitActivity))
102103 {
103104 Aggregations = new AggregationDictionary
104105 {
105- { "average_per_child", new AverageAggregation("average_per_child", "confidenceFactor") },
106- { "max_per_child", new MaxAggregation("max_per_child", "confidenceFactor") },
107- { "min_per_child", new MinAggregation("min_per_child", "confidenceFactor") },
106+ {"average_per_child", new AverageAggregation("average_per_child", "confidenceFactor")},
107+ {"max_per_child", new MaxAggregation("max_per_child", "confidenceFactor")},
108+ {"min_per_child", new MinAggregation("min_per_child", "confidenceFactor")},
108109 }
109110 }
110111 }
@@ -143,52 +144,91 @@ Now that's much cleaner! Assigning an `*Aggregation` type directly to the `Aggre
143144 on a search request works because there are implicit conversions within NEST to handle this for you.
144145
145146[float]
146- === Aggregating over a collection of aggregations
147+ === Mixed usage of object initializer and fluent
147148
148- An advanced scenario may involve an existing collection of aggregation functions that should be set as aggregations
149- on the request. Using LINQ's `.Aggregate()` method, each function can be applied to the aggregation descriptor
150- `childAggs` below) in turn, returning the descriptor after each function application.
149+ Sometimes its useful to mix and match fluent and object initializer, the fluent Aggregations method therefore
150+ also accepts `AggregationDictionary` directly.
151151
152152==== Fluent DSL example
153153
154154[source,csharp]
155155----
156- var aggregations = new List<Func<AggregationContainerDescriptor<CommitActivity>, IAggregationContainer>> <1>
156+ s => s
157+ .Aggregations(new ChildrenAggregation("name_of_child_agg", typeof(CommitActivity))
157158{
158- a => a.Average("average_per_child", avg => avg.Field(p => p.ConfidenceFactor)),
159- a => a.Max("max_per_child", avg => avg.Field(p => p.ConfidenceFactor)),
160- a => a.Min("min_per_child", avg => avg.Field(p => p.ConfidenceFactor))
161- };
159+ Aggregations =
160+ new AverageAggregation("average_per_child", Field<CommitActivity>(p => p.ConfidenceFactor))
161+ && new MaxAggregation("max_per_child", Field<CommitActivity>(p => p.ConfidenceFactor))
162+ && new MinAggregation("min_per_child", Field<CommitActivity>(p => p.ConfidenceFactor))
163+ })
164+ ----
162165
163- return s => s
164- .Aggregations(aggs => aggs
165- .Children<CommitActivity>("name_of_child_agg", child => child
166- .Aggregations(childAggs =>
167- aggregations.Aggregate(childAggs, (acc, agg) => { agg(acc); return acc; }) <2>
168- )
169- )
170- );
166+ [float]
167+ === Binary operators off the same descriptor
168+
169+ For dynamic aggregation building using the fluent syntax it can be useful to abstract to methods as much as possible.
170+ You can use the binary operator `&&` on the same descriptor to compose the graph. Each side of the
171+ binary operation can return null dynamically.
172+
173+ [source,csharp]
174+ ----
175+ s => s
176+ .Aggregations(aggs => aggs
177+ .Children<CommitActivity>("name_of_child_agg", child => child
178+ .Aggregations(Combine)
179+ )
180+ )
171181----
172- <1> a list of aggregation functions to apply
173- <2> Using LINQ's `Aggregate()` function to accumulate/apply all of the aggregation functions
174182
175- Combining multiple `AggregationDescriptor` is also possible using the bitwise `&&` operator
183+ [float]
184+ === Returning a different AggregationContainer in fluent syntax
185+
186+ All the fluent selector expects is an `IAggregationContainer` to be returned. You could abstract this to a
187+ method returning `AggregationContainer` which is free to use the object initializer syntax
188+ to compose that `AggregationContainer`.
189+
190+ [source,csharp]
191+ ----
192+ s => s
193+ .Aggregations(aggs => aggs
194+ .Children<CommitActivity>("name_of_child_agg", child => child
195+ .Aggregations(childAggs => Combine())
196+ )
197+ )
198+ ----
199+
200+ [float]
201+ === Aggregating over a collection of aggregations
202+
203+ An advanced scenario may involve an existing collection of aggregation functions that should be set as aggregations
204+ on the request. Using LINQ's `.Aggregate()` method, each function can be applied to the aggregation descriptor
205+ `childAggs` below) in turn, returning the descriptor after each function application.
176206
177207[source,csharp]
178208----
179- var aggregations = new AggregationContainerDescriptor<CommitActivity>()
180- .Average("average_per_child", avg => avg.Field(p => p.ConfidenceFactor))
181- .Max("max_per_child", avg => avg.Field(p => p.ConfidenceFactor))
182- && new AggregationContainerDescriptor<CommitActivity>()
183- .Min("min_per_child", avg => avg.Field(p => p.ConfidenceFactor));
209+ var aggregations =
210+ new List<Func<AggregationContainerDescriptor<CommitActivity>, IAggregationContainer>> <1>
211+ {
212+ a => a.Average("average_per_child", avg => avg.Field(p => p.ConfidenceFactor)),
213+ a => a.Max("max_per_child", avg => avg.Field(p => p.ConfidenceFactor)),
214+ a => a.Min("min_per_child", avg => avg.Field(p => p.ConfidenceFactor))
215+ };
184216
185217return s => s
186218 .Aggregations(aggs => aggs
187219 .Children<CommitActivity>("name_of_child_agg", child => child
188- .Aggregations(childAggs => aggregations)
220+ .Aggregations(childAggs =>
221+ aggregations.Aggregate(childAggs, (acc, agg) =>
222+ {
223+ agg(acc);
224+ return acc;
225+ }) <2>
226+ )
189227 )
190228 );
191229----
230+ <1> a list of aggregation functions to apply
231+ <2> Using LINQ's `Aggregate()` function to accumulate/apply all of the aggregation functions
192232
193233[[aggs-vs-aggregations]]
194234[float]
@@ -222,7 +262,7 @@ the `Average` and `Max` sub aggregations.
222262
223263[source,csharp]
224264----
225- response.IsValid.Should().BeTrue ();
265+ response.ShouldBeValid ();
226266
227267var childAggregation = response.Aggs.Children("name_of_child_agg");
228268
0 commit comments