0

我需要在一定时间范围内绘制price_per_unitquantitiy的交易Volume-Weighted Average Prive (VWAP)Elasticsearch Aggregations:体积加权平均价格

作为聚合的结果,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_unitquantity产品。因此VWAP = sum(price_per_unit * quantity)/sum(quantity)

+1

听起来像[累积总和聚合](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html)会有所帮助。 –

+0

你能想出一个来自上面问题的代码示例吗?我试图查看累计总和,但无法真正实现它。 –

+0

我提供的链接中有一个示例。由于您未提供任何测试数据,期望的输出和索引映射,因此我无法提供比此更好的示例。 –

回答

2
DELETE test 
PUT test 
{ 
    "mappings": { 
    "trade": { 
     "properties": { 
     "trade_id": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "product_id": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "quantity": { 
      "type": "double" 
     }, 
     "execution_time": { 
      "type": "date" 
     }, 
     "price_per_unit": { 
      "type": "double" 
     } 
     } 
    } 
    } 
} 

POST test/trade/_bulk 
{"index":{}} 
{"execution_time":"2016-11-18T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-18T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-19T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-20T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-20T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-20T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-21T22:45:27Z","quantity":10,"price_per_unit":5} 
{"index":{}} 
{"execution_time":"2016-11-21T22:45:27Z","quantity":10,"price_per_unit":5} 

POST test/trade/_search 
{ 
    "size": 0, 
    "aggs": { 
    "sales_per_day": { 
     "date_histogram": { 
     "field": "execution_time", 
     "interval": "day" 
     }, 
     "aggs": { 
     "sales": { 
      "sum": { 
      "script": { 
       "lang": "groovy", 
       "inline": "doc['quantity'] * doc['price_per_unit']" 
      } 
      } 
     }, 
     "cumulative_sales": { 
      "cumulative_sum": { 
      "buckets_path": "sales" 
      } 
     } 
     } 
    } 
    } 
} 

而且您需要启用inline scripting for groovy

+0

谢谢,这真的很有帮助! –