ElasticSearch .NET Nest子聚合

时间:2015-01-26 15:49:13

标签: elasticsearch aggregation nest

考虑以下聚合和子聚合:

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

查询工作正常,我得到了结果。

我可以通过以下方式访问顶部聚合:

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

根据文档,我应该能够得到子集:

var subAgg = agg.Aggs.Sum("sumImpressionCount");
但是,阿格斯甚至不是一个共产主义的财产 - 任何想法?

修改

这是我能够进入子聚合的唯一方式:

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

......但这似乎过于复杂。这是ES查询:

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

这是部分回应:

"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 个答案:

答案 0 :(得分:2)

文档错误。我们会尽快解决这个问题。

以下是访问聚合的正确方法:

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

foreach(var term in terms.Items)
{
    var sum = term.Sum("sumImpressionCount");
}