How to perform Sub Aggregation using NEST?

余生长醉 提交于 2020-05-17 06:48:30

问题


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

Below is my attempt

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

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

My DTO is as under

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

An equivalent DSL yields

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

What is the mistake that I am making?


回答1:


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.



来源:https://stackoverflow.com/questions/61344612/how-to-perform-sub-aggregation-using-nest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!