# Elastic Search: Composite Aggregations with Cardinality in NEST

**URL:** <https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602>\
**Category:** Elasticsearch\
**Created:** [April 11, 2020, 12:18pm UTC](https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602 "2020-04-11T12:18:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vignesh\_Theyagarajan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vignesh_theyagarajan/32/65687_2.png) [@Vignesh\_Theyagarajan](https://discuss.elastic.co/u/Vignesh_Theyagarajan)\
**Post date:** [April 11, 2020, 12:18pm UTC](https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602/1 "2020-04-11T12:18:33Z")

</div>

I'm using the composite and terms aggregations to get grouped results based on a given field. I'm also using Cardinality aggregation to get the overall count of the aggregation buckets.

Find below the request query I'm sending to get the corresponding response.

**Request** :

```auto
"aggs": {
    "myfield_comp_agg": {
      "aggs": {
        "myfield": {
          "aggs": {
            "myfield_tophits": {
              "top_hits": {
                "size": 1
              }
            }
          },
          "terms": {
            "field": "myfield",
            "size": 10
          }
        }
      },
      "composite": {
        "after": {
          "myfield_comp_terms_agg": ""
        },
        "sources": [
          {
            "myfield_comp_terms_agg": {
              "terms": {
                "field": "myfield"
              }
            }
          }
        ]
      }
    },
    "Count_agg": {
      "cardinality": {
        "field": "myfield"
      }
    }
  }

```

**Response** :

```auto
{
  ...,
  "aggregations" : {
    "Count_agg" : {
      "value" : 33
    },
    "myfield_comp_agg" : {
      "after_key" : {
        "myfield_comp_terms_agg" : "value10"
      },
      "buckets" : [
        {
          "key" : {
            "DocId_comp_terms_agg" : "value1"
          },
          "doc_count" : 1,
          "DocId" : {...}
        },
        {...},
        {...},
        {...}
      ]
    }
  }
}

```

I used Kibana to check the query and it works fine for me.

However I'm not sure how to use this cardinality aggregator in my NEST object syntax.

Here is my code:

```auto
var termsAggregation = new TermsAggregation(GetTermsAggregationName(aggregationField)) {
                Field = aggregationField,
                Size = takeCount
            };

            var topHitsAggregation = new TopHitsAggregation(GetTopHitsAggregationName(aggregationField)) {
                Size = aggregationFieldCount
            };                
            var termsAggregationContainer = new AggregationContainer {
                Terms = termsAggregation,
                Aggregations = topHitsAggregation
            };
            var subAggregations = new Dictionary<string, IAggregationContainer>() {
                { aggregationField, termsAggregationContainer}
            };

            var compositeKey = new Dictionary<string, object>() {
                { GetCompositeTermsAggregationName(aggregationField), aggregationSkipValue }
            };
            var termsSource = new TermsCompositeAggregationSource(GetCompositeTermsAggregationName(aggregationField)) {
                Field = aggregationField
            };
            var compositeAggregation = new CompositeAggregation(GetCompositeAggregationName(aggregationField)) {
                After = new CompositeKey(compositeKey),
                Sources = new List<TermsCompositeAggregationSource> { termsSource },
                Aggregations = subAggregations
            };

var searchRequest = new SearchRequest(request.IndexName)
            {
                From = request.SkipCount,
                Size = request.TakeCount
            };
searchRequest.Aggregations = compositeAggregation;
ElasticSearchClient.Search<T>(searchRequest);

```

I'd greatly appreciate any help.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 13, 2020, 5:24am UTC](https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602/2 "2020-04-13T05:24:13Z")

</div>

Take a look at the [writing aggregations documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-aggregations.html) for the client; it has some suggestions for more terse object initializer syntax, such as using `AggregationDictionary` and `&&`ing aggregations to combine them.

Here's an example that matches the request query

```auto
var client = new ElasticClient();	

AggregationDictionary aggs = new CompositeAggregation("myfield_comp_agg")
{
	After = new CompositeKey(new Dictionary<string, object>
	{
		{ "myfield_comp_terms_agg", string.Empty }
	}),
	Sources = new ICompositeAggregationSource[] 
	{
		new TermsCompositeAggregationSource("myfield_comp_terms_agg")
		{
			Field = "myfield"
		}
	},
	Aggregations = new TermsAggregation("myfield")
	{
		Field = "myfield",
		Size = 10,
		Aggregations = new TopHitsAggregation("myfield_tophits")
		{
			Size = 1
		}
	}
} && new CardinalityAggregation("Count_agg", "myfield");

var searchRequest = new SearchRequest("my_index")
{
	Aggregations = aggs
};

var searchResponse = client.Search<object>(searchRequest);

```

---

<div class="post-metadata">

**Author:** ![Vignesh\_Theyagarajan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vignesh_theyagarajan/32/65687_2.png) [@Vignesh\_Theyagarajan](https://discuss.elastic.co/u/Vignesh_Theyagarajan)\
**Post date:** [April 15, 2020, 10:05am UTC](https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602/3 "2020-04-15T10:05:24Z")

</div>

Great!! It worked.. Thanks a lot Russ.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 13, 2020, 10:05am UTC](https://discuss.elastic.co/t/elastic-search-composite-aggregations-with-cardinality-in-nest/227602/4 "2020-05-13T10:05:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
