I have the following elasticsearch query. It's being executed as part of AWS Amplify serverless backend.
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.
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. Please check below query:
"aggs": {
"points": {
"date_histogram": {
"field": "createdAt",
"interval": "day"
},
"aggs": {
"points": {
"sum": {
"field": "points"
}
}
}
},
"total":{
"sum_bucket": {
"buckets_path": "points>points"
}
}
}