5

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

1 Answer 1

2

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");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the response Greg! I tried your suggestion, but now I'm getting a null ref exception. Please see my edit above, and let me know if that's a good solution.
@AdamSweeney Sorry about that, I gave you bad example :). Just edited my answer; the new example should work and simplify things a bit for you. Hope that helps.
the terms var is type Nest.Bucket<KeyItem>, how can I iterate that?
oh, sorry. terms.Items!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.