Elasticsearch - 如何通过术语获得字段总和?

时间:2015-06-19 21:09:21

标签: elasticsearch nest

我试图得到一些字段的总和,用term(比方说,具有id的特定用户的文件数)。

首先我试过了:

_mainManager.Client.Search<object>
            (q => q
            .Type("Mail")
            .Filter(c => c.Term("SentMail_Sender_Id", userId))
            .Aggregations(a => a.Terms("sum", g => g.Field("SentMail_Upload_Files_Count")))
            .Size(1));

但在Agg属性中没有运气:(

_mainManager.Client.Search<object>
            (q => q
            .Type("Mail")
            .Aggregations(a => a.Filter("fil", b => b.Filter(c => c.Term("SentMail_Sender_Id", userId))).Sum("sum", f => f.Field("SentMail_Upload_Files_Count"))));

但是,再次,那里没有运气。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

以下代码为您提供了我需要的结果:

PUT /mail/message/1
{
  "SentMail_Sender_Id":1,
  "SentMail_Upload_Files_Count":10
}

PUT /mail/message/2
{
  "SentMail_Sender_Id":1,
  "SentMail_Upload_Files_Count":2
}

PUT /mail/message/3
{
  "SentMail_Sender_Id":2,
  "SentMail_Upload_Files_Count":7
}

GET /mail/_search?search_type=count
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "SentMail_Sender_Id": 1
        }
      }
    }
  },
  "aggs": {
    "total": {
      "stats": {
        "field": "SentMail_Upload_Files_Count"
      }
    }
  }
}

回复是:

"aggregations": {
      "total": {
         "count": 2,
         "min": 2,
         "max": 10,
         "avg": 6,
         "sum": 12
      }
   }