Hi,
I am trying some aggregation code in C# code in dotnet8.0
using Elastic.Clients.Elasticsearch library version 8.15.10
Previously, when using NEST this was used as Composite aggregation,
.Aggregations(
a => a.Composite(c => c
.Sources(cs => cs
.DateHistogram("date", dh => dh
.Field(f => f.EventDate)
.CalendarInterval(CalendarInterval.Day)
.Format("yyyy-MM-dd")
)
.Terms("tenant", t => t
.Field(f => f.TenantId)
)
)
)
)
But now, when I have tried a lot to reproduce in new Elastic.Clients.Elasticsearch library version 8.15.10, i am unable to succeed.
i was able to figure out like this for simple aggregation
.Aggregations(a =>a.
Add("hits_over_time", dh => dh.DateHistogram(a=>a
.Field(p => p.EventDate)
.CalendarInterval(CalendarInterval.Day)
.Format("yyyy-MM-dd")
)
))
But unable to get it for composite aggregation.
I feel availablity of new dotnet client document will greatly help
So, can someone guide me how to reproduce this scenario?
flobernd
(Florian Bernd)
October 29, 2024, 8:09am
2
Hi @MARYALA_VINAY_KUMAR ,
this complex case scenario (nested aggregations) is documented here:
flobernd
(Florian Bernd)
October 29, 2024, 8:19am
3
I think we have to improve usability of this. The composite aggregation is a little bit different and the code generation does not output the best possible API. Something like this should work:
var response = await client.SearchAsync<Person>(s => s
.Aggregations(aggs => aggs
.Add("composite", agg => agg
.Composite(composite => composite
.Sources([
new Dictionary<string, CompositeAggregationSource>
{
{ "date", new CompositeAggregationSource
{
DateHistogram = new CompositeDateHistogramAggregation
{
//...
}
} },
{ "terms", new CompositeAggregationSource
{
Terms = new CompositeTermsAggregation
{
//...
}
} }
}
])
)
)
)
);
We already have an open issue for this:
opened 04:21PM - 10 Jun 24 UTC
8.x
Usability
Area: Generator
Category: Feature
**Is your feature request related to a problem? Please describe.**
There doesn'… t seem to be a fluent API to define composite aggregation sources.
**Describe the solution you'd like**
I'd like an API similar to `.MultiTerms(...)`.
```c#
var response = await client.SearchAsync<Foo>(search => search
.Aggregations(aggregations => aggregations
.Add("foo", aggregation => aggregation
.MultiTerms(multiTerms => multiTerms
.Terms(
terms => terms
.Field(foo => foo.Bar),
terms => terms
.Field(foo => foo.Baz)
)
)
)
)
// ...
```
**Describe alternatives you've considered**
Currently using the non-fluent API. I was a bit confused initially by the method taking a `ICollection<IDictionary<string, CompositeAggregationSource>>` parameter.
```c#
var response = await client.SearchAsync<Foo>(search => search
.Aggregations(aggregations => aggregations
.Add("foo", aggregation => aggregation
.Composite(composite => composite
.Sources(new[]
{
new Dictionary<string, CompositeAggregationSource>()
{
{ "bar", new() { Terms = new() { Field = "bar" } } }
},
new Dictionary<string, CompositeAggregationSource>()
{
{ "baz", new() { Terms = new() { Field = "baz" } } }
}
})
)
)
)
// ...
```
**Additional context**
I'm using a composite aggregation because I need to stream all buckets.
The example at https://github.com/elastic/elasticsearch-net/issues/7822#issuecomment-2038090501 was quite helpful.