# Trouble in Composite aggregation in new ElasticSearchClient Library in dotnet

**URL:** https://discuss.elastic.co/t/trouble-in-composite-aggregation-in-new-elasticsearchclient-library-in-dotnet/369705
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [October 29, 2024, 7:13am UTC](https://discuss.elastic.co/t/trouble-in-composite-aggregation-in-new-elasticsearchclient-library-in-dotnet/369705 "2024-10-29T07:13:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MARYALA\_VINAY\_KUMAR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maryala_vinay_kumar/32/138765_2.png) [@MARYALA\_VINAY\_KUMAR](https://discuss.elastic.co/u/MARYALA_VINAY_KUMAR)
#### Post date: [October 29, 2024, 7:13am UTC](https://discuss.elastic.co/t/trouble-in-composite-aggregation-in-new-elasticsearchclient-library-in-dotnet/369705/1 "2024-10-29T07:13:23Z")

</div>

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,

```auto
.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

```auto
  .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?

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [October 29, 2024, 8:09am UTC](https://discuss.elastic.co/t/trouble-in-composite-aggregation-in-new-elasticsearchclient-library-in-dotnet/369705/2 "2024-10-29T08:09:55Z")

</div>

Hi @MARYALA_VINAY_KUMAR ,

this complex case scenario (nested aggregations) is documented here:

> **[Aggregation examples | Elasticsearch .NET Client \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/net-api/master/aggregations.html#_sub_aggregation)**

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [October 29, 2024, 8:19am UTC](https://discuss.elastic.co/t/trouble-in-composite-aggregation-in-new-elasticsearchclient-library-in-dotnet/369705/3 "2024-10-29T08:19:47Z")

</div>

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:

```csharp
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:

> <https://github.com/elastic/elasticsearch-net/issues/8227>
>
> \*\*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.
