[英]Calculating sum of nested fields with date_histogram aggregation in Elasticsearch
I'm having trouble getting the sum of a nested field in Elasticsearch using a date_histogram, and I'm hoping somebody can lend me a hand. 我在使用date_histogram获取Elasticsearch中的嵌套字段的总和时遇到了麻烦,我希望有人可以帮我一把。
I have a mapping that looks like this: 我有一个如下所示的映射:
"client" : {
// various irrelevant stuff here...
"associated_transactions" : {
"type" : "nested",
"include_in_parent" : true,
"properties" : {
"amount" : {
"type" : "double"
},
"effective_at" : {
"type" : "date",
"format" : "dateOptionalTime"
}
}
}
}
I'm trying to get a date_histogram that shows total revenue by month across all clients--ie a time series showing the sum associated_transactions.amount in a histogram determined by associated_transactions.effective_date. 我正在尝试获得一个date_histogram,它显示所有客户的按月总收入 - 即显示由associated_transactions.effective_date确定的直方图中的和associated_transactions.amount的时间序列。 I tried running this query: 我试过运行这个查询:
{
"query": {
// ...
},
"aggregations": {
"revenue": {
"date_histogram": {
"interval": "month",
"min_doc_count": 0,
"field": "associated_transactions.effective_at"
},
"aggs": {
"monthly_revenue": {
"sum": {
"field": "associated_transactions.amount"
}
}
}
}
}
}
But the sum it's giving me isn't right. 但它给我的总和是不对的。 It seems that what ES is doing is finding all clients who have any transaction in a given month, then summing all of the transactions (from any time) for those clients. 似乎ES正在做的是找到在给定月份内有任何交易的所有客户,然后为这些客户总结所有交易(从任何时间)。 That is, it's a sum of the amount spent in the lifetime of a client who made a purchase in a given month, not the sum of purchases in a given month. 也就是说,它是在给定月份内进行购买的客户的生命周期中花费的金额的总和,而不是在给定月份中购买的总和。
Is there any way to get the data I'm looking for, or is this a limitation in how ES handles nested fields? 有没有办法获取我正在寻找的数据,或者这是ES如何处理嵌套字段的限制?
Thanks very much in advance for your help! 非常感谢您的帮助!
David 大卫
Try this? 试试这个?
{
"query": {
// ...
},
"aggregations": {
"revenue": {
"date_histogram": {
"interval": "month",
"min_doc_count": 0,
"field": "associated_transactions.effective_at"
"aggs": {
"monthly_revenue": {
"sum": {
"field": "associated_transactions.amount"
}
}
}
}
}
}
}
ie move the "aggs" key into the "date_histogram" field. 即将“aggs”键移动到“date_histogram”字段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.