Here's an example. Starting with converting the lamdba expression to one with a statement body
var client = new ElasticClient();
var includeMinMax = true;
client.Search<MyDocument>(s => s
.Aggregations(agg =>
{
agg.Cardinality("dedup", cd => cd
.Field(fi => fi.AddressHash)
.PrecisionThreshold(40000)
)
.Filter("dontDedup", fl => fl
.Filter(ft => ft
.Term(te => te.AddressHash, "0")
)
);
if (includeMinMax)
{
agg.Min("min_lat", fl => fl.Field(fi => fi.Latitude))
.Max("max_lat", fl => fl.Field(fi => fi.Latitude))
.Min("min_lon", fl => fl.Field(fi => fi.Longitude))
.Max("max_lon", fl => fl.Field(fi => fi.Longitude));
}
return agg;
})
);
You can then extract the lambda statement body out to a method
client.Search<MyDocument>(s => s
.Aggregations(agg => BuildAggregations(agg, includeMinMax))
);
private static IAggregationContainer BuildAggregations(AggregationContainerDescriptor<MyDocument> agg, bool includeMinMax)
{
agg.Cardinality("dedup", cd => cd
.Field(fi => fi.AddressHash)
.PrecisionThreshold(40000)
)
.Filter("dontDedup", fl => fl
.Filter(ft => ft
.Term(te => te.AddressHash, "0")
)
);
if (includeMinMax)
{
agg.Min("min_lat", fl => fl.Field(fi => fi.Latitude))
.Max("max_lat", fl => fl.Field(fi => fi.Latitude))
.Min("min_lon", fl => fl.Field(fi => fi.Longitude))
.Max("max_lon", fl => fl.Field(fi => fi.Longitude));
}
return agg;
}
Or even the entire lambda
client.Search<MyDocument>(s => s
.Aggregations(BuildAggregations(includeMinMax))
);
private static Func<AggregationContainerDescriptor<MyDocument>, IAggregationContainer> BuildAggregations(bool includeMinMax)
{
return agg =>
{
agg.Cardinality("dedup", cd => cd
.Field(fi => fi.AddressHash)
.PrecisionThreshold(40000)
)
.Filter("dontDedup", fl => fl
.Filter(ft => ft
.Term(te => te.AddressHash, "0")
)
);
if (includeMinMax)
{
agg.Min("min_lat", fl => fl.Field(fi => fi.Latitude))
.Max("max_lat", fl => fl.Field(fi => fi.Latitude))
.Min("min_lon", fl => fl.Field(fi => fi.Longitude))
.Max("max_lon", fl => fl.Field(fi => fi.Longitude));
}
return agg;
};
}
For the object initializer syntax
var client = new ElasticClient();
var includeMinMax = true;
var aggs = new Dictionary<string, AggregationContainer>{
{ "dedup", new CardinalityAggregation("dedup", Infer.Field<MyDocument>(f => f.AddressHash))
{
PrecisionThreshold = 40000
}
},
{ "filter", new FilterAggregation("dontDedup")
{
Filter = new TermQuery
{
Field = Infer.Field<MyDocument>(f => f.AddressHash),
Value = "0"
}
}
}
};
if (includeMinMax)
{
aggs.Add(...);
}
client.Search<MyDocument>(new SearchRequest<MyDocument>
{
Aggregations = new AggregationDictionary(aggs)
});
Using a dictionary can be long winded, so aggregations can be combined with &&
var client = new ElasticClient();
var includeMinMax = true;
AggregationBase aggs =
new CardinalityAggregation("dedup", Infer.Field<MyDocument>(f => f.AddressHash))
{
PrecisionThreshold = 40000
};
aggs &= new FilterAggregation("dontDedup")
{
Filter = new TermQuery
{
Field = Infer.Field<MyDocument>(f => f.AddressHash),
Value = "0"
}
};
if (includeMinMax)
{
// && additional aggs
}
client.Search<MyDocument>(new SearchRequest<MyDocument>
{
Aggregations = aggs
});