【发布时间】:2017-04-13 12:36:20
【问题描述】:
我需要绘制在特定时间范围内具有price_per_unit 和quantitiy 的交易的Volume-Weighted Average Prive (VWAP)。
作为聚合的结果,date_histogram 的每个桶都应该包含迄今为止发生的所有交易的 VWAP。
我不确定这是否可以使用 Elasticsearch 实现,也不确定什么是正确的方法(比如使用脚本?)?
trade 文档的基本映射非常简单:
"trade": {
"properties":
"trade_id": {"type": "string", "index": "not_analyzed"},
"product_id": {"type": "string", "index": "not_analyzed"},
"quantity": {'type': 'double'}, // number of units
"execution_time": {'type': 'date'},
"price_per_unit": {'type': 'double'},
}
}
而execution_time 应该用于date_histogram,交易的总价格是price_per_unit 和quantity 的乘积。因此VWAP = sum(price_per_unit * quantity) / sum(quantity)。
【问题讨论】:
-
听起来像cumulative sum aggregation 会有所帮助。
-
你能想出一个代码示例来解决上面的问题吗?我尝试查看累积总和,但无法真正实现。
-
我提供的链接中有一个示例。而且我无法提供比那个更好的样本,因为您没有提供任何测试数据、所需的输出和索引映射。
-
@AndreiStefan 我已经用相关映射更新了问题,
price_per_unit和quantity只是数字,所以不需要真实的样本数据;感谢您的帮助,非常感谢,因为一开始很难理解 ES 的聚合。
标签: elasticsearch elasticsearch-aggregation elasticsearch-query