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

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

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;
1 Like