Elasticsearch - 如何获得带术语的字段总和?

Elasticsearch - How to get the sum of fields with terms?

我正在尝试获取某些字段的总和,其中包含条款(比方说,具有 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"))));

但话又说回来,运气不好。 有人可以帮忙吗?

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

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