【问题标题】:How to perform SUM and count in NEST?如何在 NEST 中执行 SUM 和计数?
【发布时间】:2020-02-12 07:34:31
【问题描述】:

我有以下POCO

public class Collection
    {        
        public string Amount { get; set; }

        public string AccountId { get; set; }

        public string ReceiptId { get; set; }

    }

我在找

a) 金额总和 b) ReceiptId 计数

按 AccountId 分组使用 Nest。

由于我是 Elastic Search 和 Nest 的新手,我很难构建查询。

这是我迄今为止尝试过的

 var result = ConnectionToES.EsClient().Search<Collection>(s => s
                    .Index("collections")
                    .Aggregations(a => a

                        .Terms("ReceiptId", ts => ts
                            .Field(o => o.AccountId)
                            .Aggregations(aa => aa.Sum("Amount", sa => sa.Field(o => Convert.ToDecimal(o.Amount)))

                            )
                        )
                    )
                );

但是当我使用 LINQ 做同样的事情时,它可以工作

var res = ConnectionToES.EsClient().Search<Collection>(s => s
            .Index("collections")
            .From(0)
            .Size(1000)
            .Query(q => q.MatchAll()));

            List<Collection> objCollection = new List<Collection>();

            foreach (var hit in res.Hits)
            {
                objCollection.Add(
                    new Collection
                    {
                        Id = hit.Source.Id.ToString()
                         ,

                        Amount = string.IsNullOrEmpty(hit.Source.Amount.ToString()) ? "0" : hit.Source.Amount.ToString()
                         ,
                        AccountId = hit.Source.AccountId.ToString()
                         ,
                        ReceiptId = hit.Source.ReceiptId.ToString()

                         ,
                        CollectorId = hit.Source.CollectorId.ToString()
                         ,
                        CollectionDate = hit.Source.CollectionDate.ToString()

                    });
            }

            var aggrResult = objCollection.GroupBy(t => t.AccountId)
                           .Select(t => new
                           {
                               AccountNumber = t.Key,
                               Count = t.Count(),
                               Amount = t.Sum(item => Convert.ToDouble(item.Amount)),
                           }).ToList();

我想以 ES 方式进行相同的聚合。

【问题讨论】:

  • 你想要什么:.GroupBy(x => x.AccountId) .Select(x => new { id = x.AccountId, count = x.Count(), total = x.Sum( y => y.Amount)}) .ToList();
  • 是的,但采用 Elastic Search /NEST 样式。先生,我想使用 NEST 在 Elastic Search 中执行聚合
  • 您好,请分享es映射。请注意,es 字段区分大小写,如果您自动映射我认为 es 创建一个“receiptId”字段,而不是“ReceiptId”。此外,您还必须按如下所述更改映射,并将字段 Amount 索引为数值(聚合在 es 端进行,Convert.ToDecimal( 在您的示例中有废话)。

标签: c# elasticsearch nest


【解决方案1】:

两点帮助

  1. Amount 应该是一个数值(可能是 double?),以便对其执行求和聚合。
  2. AccountId 上的术语聚合看起来应该是 ReceiptId 以按 ReceiptId 的值存储。此外,如果 ReceiptId 是不需要全文搜索的字符串值,则应将其映射为 keyword 数据类型,这将允许聚合、排序和术语级别的查询。

【讨论】:

  • 我已经按照建议尝试了。可能是我错过了一些东西。我已经更新了问题。请帮助
  • 好的,我看错了你的问题。计数ReceiptId 与按AccountId 分组时每个存储桶中的文档计数相同 - 这就是您想要的吗?如果是,则已经为每个存储桶计算了文档计数。要阅读聚合和子聚合,请查看:elastic.co/guide/en/elasticsearch/client/net-api/current/…
猜你喜欢
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多