elastic/elasticsearch-net

How to set Aggregations for a NestedAggregation object

Closed this issue · 3 comments

Elastic.Clients.Elasticsearch version: 8.19.6

Elasticsearch version: 8.18.4

.NET runtime version: .net9

Operating system version: Cloud Cluster

Description of the problem including expected versus actual behavior:
I need a way to construct the following query

{
  "size": 0,
  "aggs": {
    "nested_metadata": {
      "nested": {
        "path": "AdditionalMetadata"
      },
      "aggs": {
        "distinct_keys": {
          "terms": {
            "field": "AdditionalMetadata.key.keyword",
            "size": 1000
          }
        }
      }
    }
  }
}

I don't see a way to set the aggs with in the nested object. Here is what I have

var searchRequest = new SearchRequest
{
    Size = 0,
    Aggregations = new Dictionary<string, Aggregation>
    {
        {
            "nested_metadata", new NestedAggregation()
            {
                Path = "AdditionalMetadata",
            }
        }
    },
};

I am expecting the NestedAggregation to have a Aggregations property of type IDictionary<string, Aggregation>? but I can't find it. I tried to look for anther class that could be inherited from NestedAggregation but can't find any.

@flobernd what is the trick to set the nested aggregation using the library?

@MikeAlhayek ,

The trick is to use:

new Aggregation
{
    Nested = new NestedAggregation(...),
    Aggregations = [...]
}

If you look at the JSON payload, it has "nested" and "aggs" on the same level. The .NET client most of the times reflects the same structure 1:1.

Thanks a lot for your feedback.