我有以下POCO
public class Collection
{
public string Amount { get; set; }
public string AccountId { get; set; }
public string ReceiptId { get; set; }
}
我正在寻找
a)金额总和b)收据编号
通过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方式进行相同的聚合。
提前感谢。
一些帮助的指针
Amount
应该是一个数值(也许是double
?),以便对其进行求和求和。AccountId
上的术语聚合看起来应该是ReceiptId
,以ReceiptId
的值进行存储。另外,如果ReceiptId
是不需要全文搜索的字符串值,则应将其映射为keyword
数据类型,这将允许聚合,排序和术语级别查询。