我在ElasticSearch索引some_index
中有以下数据。
[ {
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 1,
"cart_status": "new",
"grandTotal": 12,
"event": "some_event",
"timestamp": "2022-12-01T00:00:00.000Z"
}
}
},
{
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 1,
"cart_status": "paid",
"grandTotal": 12,
"event": "some_event",
"timestamp": "2022-12-02T00:00:00.000Z"
}
}
},
{
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 2,
"cart_status": "new",
"grandTotal": 23,
"event": "some_event",
"timestamp": "2022-12-01T00:00:00.000Z"
}
}
},
{
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 2,
"cart_status": "paid",
"grandTotal": 23,
"event": "some_event",
"timestamp": "2022-12-04T00:00:00.000Z"
}
}
},
{
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 3,
"cart_status": "new",
"grandTotal": 17,
"event": "some_event",
"timestamp": "2022-12-01T00:00:00.000Z"
}
}
},
{
"_index": "some_index",
"_source": {
"cart": {
"cart_id": 3,
"cart_status": "new",
"grandTotal": 17,
"event": "some_event",
"timestamp": "2022-12-04T00:00:00.000Z"
}
}
}
]
我想得到的是grandTotals
与给定时间范围内每个购物车的最新cart_status
之和。
根据上面的示例,timestamp >= 2022-12-01 00:00:00 and timestamp<= 2022-12-03 00:00:00
的结果应该类似于cart_status:new, sum grandTotal: 40
,因为在该时间范围内,最新状态new具有cart_id3和2。
和cart_status:paid, sum grandTotal: 12
以及这个,因为已支付是仅cart_id=1的最新状态。
我尝试使用sub-aggregation on top_result, top_hits
,但ElasticSearch抱怨"Aggregator [top_result] of type [top_hits] cannot accept sub-aggregations"
此外,我还尝试使用collapse
来获取最新的状态,但根据文档,也不可能对collapse
的结果进行聚合。
有人能帮我解决这个问题吗,这似乎是一个常见的计算,但在ElasticSearch中不是很微不足道。
在SQL中,使用窗口函数很容易做到这一点。
我想避免将中间数据持久化到另一个索引中。因为我需要动态查询,因为用户可能希望获得任何时间范围的计算结果。
1条答案
按热度按时间2uluyalo1#
你可以尝试这下面的方法. whitember,为card_status,sum值将是52因为它包括card_id 1它有"新"作为卡状态连同2和3为给定的时间戳.