如何在bucket聚合中进行过滤聚合?

yqlxgs2m  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(0)|浏览(276)

我有以下要求。我有一些记录看起来像在下面(作为一个例子)

agreementid = 1, lastdispositioncode = PTP , feedbackdate = 30/11/2020 
agreementid = 1, lastdispositioncode = PTP  , feedbackdate = 29/11/2020
agreementid = 1, lastdispositioncode = BPTP , feedbackdate = 21/11/2020
agreementid = 2, lastdispositioncode = BPTP  , feedbackdate = 29/11/2020
agreementid = 2, lastdispositioncode = BPTP , feedbackdate = 11/11/2020
agreementid = 3, lastdispositioncode = SBPTP  , feedbackdate = 24/11/2020

首先,我们必须
协议ID上的第一组,
然后按反馈日期排序以获取最新记录
在那之后,我们要计算一下lastdispositioncode的数量。
以上数据将给出以下结果

ptp = 1 (since latest record of PTP is on 30/11/2020 for AggrementID = 1)
bptp = 3 (since 

                for AggrementID = 1, latest record of BPTP is on 21/11/2020
                for AggrementID = 2, latest record of BPTP is on 29/11/2020
                for AggrementID = 3, latest record of SBPTP is on 24/11/2020)

我尝试了以下程序,但查询失败。

class Program
{
    static void Main(string[] args)
    {
        var ptpDispositionCodes = new TermsQuery
        {
            IsVerbatim = true,
            Field = "lastdispositioncode",
            Terms = new string[] { "PTP" },
        };

        var bptpDispositionCodes = new TermsQuery
        {
            IsVerbatim = true,
            Field = "lastdispositioncode",
            Terms = new string[] { "BPTP","SBPTP" },
        };

        ISearchResponse<TestReportModel> searchResponse =
            ConnectionToES.EsClient()
            .Search<TestReportModel>
            (s => s
                .Index("feedbackdata")
                .From(0)
                .Size(50000)
                .Query(q =>q.MatchAll())
                .Aggregations(a => a
                    .Terms("Agreement_ID", t => t
                        .Field(f => f.agreementid.Suffix("keyword"))
                        .Aggregations(aa => aa
                            .TopHits("latest_feedbackdate_sort", th => th.Sort(so => so.Descending(f => f.feedbackdate))

                                .Size(1) //get the latest record 
                            )

                        )
                        .Aggregations(fa => fa

                            .Filter("ptp_aggs", f => f.Filter(fd => ptpDispositionCodes))
                            .Filter("bptp_aggs", f => f.Filter(fd => bptpDispositionCodes))                         
                        )
                    )
                )
            );

        var ptpDocCount = ((Nest.SingleBucketAggregate)trailSearchResponse.Aggregations["ptp_aggs"]).DocCount;
        var bptpDocCount = ((Nest.SingleBucketAggregate)trailSearchResponse.Aggregations["bptp_aggs"]).DocCount;
    }
}

dto公司

public class TestReportModel
{
    public string agreementid { get; set; }
    public string trailstatus { get; set; }        
    public string lastdispositioncode { get; set; }        
}

public class TestOutputAPIModel
{
    public List<TestModel> TestModelDetail { get; set; }    
}

public class TestModel
{   
    public string TrailStatus { get; set; }
    public int NoOfAccounts { get; set; }

}

以下是我目前收到的答复

可以看出有两个问题
a) “agreementid”上的聚合只发生在前10个记录上(但系统中有许多唯一的agreementid,比如超过5/6个lac)
b) ptp\u aggs/bptp\u aggs归零,因为有匹配的lastdispositioncodes。
下一步我能试试什么?
编辑
这是我从debuginformation获得的原始查询
apicall.requestbodyinbytes

{"aggs":{"Agreement_ID":{"aggs":{"ptp_aggs":{"filter":{"terms":{"lastdispositioncode":["ptp"]}}},"bptp_aggs":{"filter":{"terms":{"lastdispositioncode":["bptp"]}}},"paid_aggs":{"filter":{"terms":{"lastdispositioncode":["clmpd","dib","exceemi","odp","partpaid","pr"]}}}},"terms":{"field":"agreementid.keyword"}}},"from":0,"query":{"match_all":{}},"size":50000}

apicall.responsebodyinbytes字节

"aggregations" : {
    "sterms#Agreement_ID" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 658,
      "buckets" : [
        {
          "key" : "2233585285",
          "doc_count" : 513,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 513
          }
        },
        {
          "key" : "ABCD123456",
          "doc_count" : 95,
          "filter#bptp_aggs" : {
            "doc_count" : 8
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 87
          }
        },
        {
          "key" : "8728441356",
          "doc_count" : 53,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 53
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "5385541121",
          "doc_count" : 39,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 39
          }
        },
        {
          "key" : "VEH001001",
          "doc_count" : 30,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "2671278479",
          "doc_count" : 29,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 29
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "4937845646",
          "doc_count" : 27,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "4472873928",
          "doc_count" : 25,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "LD1736377086",
          "doc_count" : 24,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 0
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        },
        {
          "key" : "5548762365",
          "doc_count" : 23,
          "filter#bptp_aggs" : {
            "doc_count" : 0
          },
          "filter#paid_aggs" : {
            "doc_count" : 23
          },
          "filter#ptp_aggs" : {
            "doc_count" : 0
          }
        }
      ]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题