如何使用NEST执行子聚合? [英] How to perform Sub Aggregation using NEST?

查看:236
本文介绍了如何使用NEST执行子聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据特定条件使用存储桶聚合来执行组文档,并为每个存储桶执行总和聚合.

I am trying to perform group documents by certain criteria with a bucket aggregation and perform a sum aggregation for each bucket.

下面是我的尝试

ISearchResponse<PaymentReportModel> paymentSearchResponse =
                ConnectionToES.EsClient()
                .Search<PaymentReportModel>
                (s => s
                    .Index("payments")                    
                    .Query(q => q.MatchAll() )

                     .Aggregations(a => a

                     .Terms("paymentstatus_types", ts => ts
                        .Field(o => o.paymentstatus)
                            .Aggregations(aa => aa
                                .Sum("sumreceiptamount", sa => sa
                                    .Field(o => o.totalreceiptamount)
                                    )
                                )
                            )
                        )
                    );

var paymentRecords = paymentSearchResponse.Documents ; // Count = 0

我得到零(0)计数,因为付款索引中有356个文档.

I am getting Zero(0) count where as there are 356 documents in payments Index.

我的DTO如下

public class PaymentReportModel
{
    public string paymentid { get; set; }
    public float? totalreceiptamount { get; set; }        
    public string paymentstatus { get; set; }       
}

等效的DSL产生量

"aggregations" : {
    "paymentstatus_types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "ReceivedByCollector",
          "doc_count" : 36,
          "sumreceiptamount" : {
            "value" : 56914.14031982422
          }
        },
        {
          "key" : "CollectionAcknowledged",
          "doc_count" : 17,
          "sumreceiptamount" : {
            "value" : 6802.75
          }
        },
        {
          "key" : "PayInSlipCreated",
          "doc_count" : 10,
          "sumreceiptamount" : {
            "value" : 4183.0
          }
        },
        {
          "key" : "CollectionSuccess",
          "doc_count" : 5,
          "sumreceiptamount" : {
            "value" : 27.0
          }
        }
      ]
    }
  }
}

我在犯什么错误?

推荐答案

因此,根据您的查询,您将不会获得文档,因为您正在使用聚合.可以肯定的是,即使您想要文档,也可以删除size:0.然后,您将获得汇总结果以及大小为10的文档.

So, based on your query you will not get documents because you are using aggregations.That's for sure, still if you want documents then you remove size:0.Then you will get aggregated result as well as documents of size 10.

这篇关于如何使用NEST执行子聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆