如果我正確理解您的要求,以下應該工作。由於我們有權訪問查詢,我們可以做任何事情來限制數據集。在我的例子,我只是說了時間戳> = 1365440000:
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"range": {
"timestamp": {
"gte": 1365440000
}
}
}
}
},
"aggs": {
"customers": {
"terms": {
"field": "customer_id"
},
"aggs": {
"order_stats": {
"stats": {
"field": "order_amount"
}
}
}
}
}
}
下面是結果:
{
"took": 32,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"customers": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1001,
"doc_count": 4,
"order_stats": {
"count": 4,
"min": 13,
"max": 15,
"avg": 13.875,
"sum": 55.5
}
},
{
"key": 1002,
"doc_count": 4,
"order_stats": {
"count": 4,
"min": 13.5,
"max": 15.5,
"avg": 14.625,
"sum": 58.5
}
}
]
}
}
}
希望它能幫助。
這不起作用。您正在使用硬編碼的時間戳過濾器範圍。那不是我正在尋找的。查詢需要查看每個客戶的最新文檔 – user3658423