Below is my sample document
{
"totalPrice": 122.24173769217346,
"createdAt": "2023-07-24T06:25:44.961",
"domain": "mystore.wix.com"
}
My requirement is to retrieve the records where the domain must match mystore.wix.com
and should be grouped by the date createdAt
with the totalPrice
sum aggregated and also sorted in descending order by the same createdAt
. I want the size of aggregation to be 10, that is only 10 buckets need to be returned and no more.
I tried the query,
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"domain": "mystore.wix.com"
}
}
]
}
},
"aggs": {
"groupByDate": {
"date_histogram": {
"field": "createdAt",
"calendar_interval": "1d",
"min_doc_count": 1,
"order": {
"_key": "desc"
}
},
"aggs": {
"totalPrice": {
"sum": {
"field": "totalPrice",
"script": {"source": "Math.round(doc['totalPrice'].value * 1000.0)/1000.0"}
}
}
}
}
}
}
But i could not see how i could restrict the aggregation size to 10. Hence i am getting a big size result. What am i missing here ?