Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch .NET Nest Sub Aggregation

Consider the following aggregation and sub aggregation:

.Aggregations(a => a
                        .Terms("ByCustomerByImpressionCount", tad => tad
                            .Field(o => o.CustomerName)
                            .OrderDescending("sumImpressionCount")
                            .Aggregations(ad => ad
                                .Sum("sumImpressionCount", sad => sad
                                    .Field(o => o.ImpressionCount)
                                )                        
                            )
                        ))

The query works fine, and I get results.

I can access the top aggregation with:

var agg = results.Aggs.Terms("ByCustomerByImpressionCount");

According to the docs, I should be able to get the sub agg with:

var subAgg = agg.Aggs.Sum("sumImpressionCount");

But Aggs isn't even a property of agg - any ideas?

EDIT:

This is the only way I've been able to get to the sub aggregation:

PopulateList(results.Aggs.Terms("CustomersByImpressionCount").Items, CustomersByImpressionCount);

private void PopulateList(IList<KeyItem> items, List<DataContainer> listToPopulate)
{
                items.ForEach(item =>
                {
                    listToPopulate
                        .Add(new DataContainer() { Name = item.Key, Sum = ((ValueMetric)item.Aggregations.First().Value).Value });
                });
}

...but this seems to be overly complicated. Here is the ES query:

"query": { 
    "bool": {
      "must": [
        { "range": { "requestedShipDate":   {
            "gte": "2014-11-26",
            "lte": "2015-02-03"
        } }

        }        
      ],
      "must_not": [
         {"terms": {
            "status": [
               "Order Shipped",
               "Canceled",
               "Completed"
            ]
         }}
      ]
    }
  },
"aggs": {
    "ByCustomerByImpressionCount": {
      "terms": {
        "field":   "customerName",
        "order": { "sumImpressionCount": "desc" }
      },
      "aggs": {
        "sumImpressionCount": { "sum":      { "field":  "impressionCount"           }} 
      }
    }}

And here is partial response:

"ByCustomerByImpressionCount": {
         "doc_count_error_upper_bound": -1,
         "sum_other_doc_count": 26785,
         "buckets": [
            {
               "key": "<One of our customers>",
               "doc_count": 1722,
               "sumImpressionCount": {
                  "value": 9583176
               }
            }
like image 571
Adam Sweeney Avatar asked Oct 31 '22 10:10

Adam Sweeney


1 Answers

The documentation is wrong. We'll fix that ASAP.

Here is the correct way to access your aggregations:

var terms = results.Aggs.Terms("ByCustomerByImpressionCount");

foreach(var term in terms.Items)
{
    var sum = term.Sum("sumImpressionCount");
}
like image 61
Greg Marzouka Avatar answered Nov 15 '22 08:11

Greg Marzouka