# ElasticSearch - .NET NEST API building multiple aggregrations with object initialization API seems to create an incorrect request with nested aggregrations

**URL:** <https://discuss.elastic.co/t/elasticsearch-net-nest-api-building-multiple-aggregrations-with-object-initialization-api-seems-to-create-an-incorrect-request-with-nested-aggregrations/49748>\
**Category:** Elasticsearch\
**Created:** [May 11, 2016, 9:44am UTC](https://discuss.elastic.co/t/elasticsearch-net-nest-api-building-multiple-aggregrations-with-object-initialization-api-seems-to-create-an-incorrect-request-with-nested-aggregrations/49748 "2016-05-11T09:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Harvey](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Paul\_Harvey](https://discuss.elastic.co/u/Paul_Harvey)\
**Post date:** [May 11, 2016, 9:44am UTC](https://discuss.elastic.co/t/elasticsearch-net-nest-api-building-multiple-aggregrations-with-object-initialization-api-seems-to-create-an-incorrect-request-with-nested-aggregrations/49748/1 "2016-05-11T09:44:30Z")

</div>

I am building the components of an elastic search query dynamically, the query contains potentially multiple aggregations.

.Net 4.5.2 Nest 2.3.1 [Elasticsearch.Net](http://Elasticsearch.Net) 2.3.1

I can successfully add multiple aggregations by repeating the following structure:

```
 var aggregations = new AggregationDictionary();
 aggregations["yyy"] = new AggregationContainer {
    Terms = new TermsAggregation("xxx")
    {
       Field = "afield"
    }
 };

aggregations["ccc"] = new AggregationContainer {
    Terms = new TermsAggregation("ddd")
    {
       Field = "anotherfield"
    }
 };

```

And then setting the Aggregrations property on the search to the aggregations variable. And all is good.

I can successfully created a single nested aggregration as follows:

```
var aggregations=new NestedAggregation("Countries") {
   Path = "MetaData.GeographicCoverage.Countries",
   Aggregations =
      new TermsAggregation("Country") {
         Field = "MetaData.GeographicCoverage.Countries.Country"
      }
 };

```

And again setting the Aggregrations property on the search to the aggregations variable and all is good.

The problem comes when I combine the two approaches to create a query with many aggregrations where one (or more) of which is nested. So the Json generated by the above nested example looks like:

```
{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
            "path": "MetaData.GeographicCoverage.Countries"
      },
      "aggs": {
        "Country": {
          "terms": {
            "field": "MetaData.GeographicCoverage.Countries.Country"
          }
        }
      }
    }
  }
}

```

Now when I combine the approaches so that the nested aggregration is added just like those in the very first code snippet:

```
var aggregations = new AggregationDictionary();

var nested = new NestedAggregation("Countries") {
   Path = "MetaData.GeographicCoverage.Countries",
   Aggregations =
      new TermsAggregation("Country") {
         Field = "MetaData.GeographicCoverage.Countries.Country"
      }
   };

aggregations["Countries"] = new AggregationContainer {
   Nested = nested
};

```

Then the Json of the query that is generated misses the actual "Country" aggregration:

```
{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
        "path": "MetaData.GeographicCoverage.Countries"
      }
    }
  }
}

```

So then - is this a bug or am I using the Nest classes incorrectly? If I am using the classes incorrectly how do I fix the code?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Paul\_Harvey](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Paul\_Harvey](https://discuss.elastic.co/u/Paul_Harvey)\
**Post date:** [May 12, 2016, 12:49pm UTC](https://discuss.elastic.co/t/elasticsearch-net-nest-api-building-multiple-aggregrations-with-object-initialization-api-seems-to-create-an-incorrect-request-with-nested-aggregrations/49748/2 "2016-05-12T12:49:26Z")

</div>

Turns out the way around the problem is to add an explicit cast of the NestedAggregration to an AggregationContainer and then to add it directly to the AggregationsDictionary.

```
var aggregations = new AggregationDictionary();
var nested =
   new NestedAggregation("Countries") {
      Path = "MetaData.GeographicCoverage.Countries",
      Aggregations =
         new TermsAggregation("Country") {
            Field = "MetaData.GeographicCoverage.Countries.Country"
         }
       };

 aggregations["Countries"] = (AggregationContainer)nested;
```

---

<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:** [July 5, 2017, 10:52pm UTC](https://discuss.elastic.co/t/elasticsearch-net-nest-api-building-multiple-aggregrations-with-object-initialization-api-seems-to-create-an-incorrect-request-with-nested-aggregrations/49748/3 "2017-07-05T22:52:06Z")

</div>


