[英]Elasticsearch query is not returning a total value of a summed field
I have the following elasticsearch query.我有以下 elasticsearch 查询。 It's being executed as part of AWS Amplify serverless backend.它作为 AWS Amplify 无服务器后端的一部分执行。
const elasticBody = {
...defaultBody,
aggs: {
points: {
date_histogram: {
field: "createdAt",
interval: "day",
},
aggs: {
points: {
sum: {
field: "points",
},
},
},
},
total: {
sum: {
field: "points",
},
},
},
};
const data = await search(index, elasticBody);
I get the following response, which is most of what I'm attempting to get, however, the 'total' value, in the lower portion of the query is not yielding a result.我得到以下响应,这是我试图获得的大部分内容,但是,查询下部的“总计”值没有产生结果。
I've been poring over the Elasticsearch documentation but I'm unable to find a solution.我一直在仔细研究 Elasticsearch 文档,但找不到解决方案。
I was expecting the following structure in the response.我期待响应中的以下结构。
count: x,
data: [{...}],
I was expecting the count to be the summed value of all the points within the returned data set.我期望计数是返回数据集中所有点的总和值。
You need to use Sum Bucket Aggregation to get total sum of return bucket response.您需要使用Sum Bucket Aggregation来获取返回存储桶响应的总和。 Please check below query:请检查以下查询:
"aggs": {
"points": {
"date_histogram": {
"field": "createdAt",
"interval": "day"
},
"aggs": {
"points": {
"sum": {
"field": "points"
}
}
}
},
"total":{
"sum_bucket": {
"buckets_path": "points>points"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.