【问题标题】:Elasticsearch Aggregations: Volume-Weighted Average PriceElasticsearch 聚合:成交量加权平均价格
【发布时间】:2017-04-13 12:36:20
【问题描述】:

我需要绘制在特定时间范围内具有price_per_unitquantitiy 的交易的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_unitquantity 的乘积。因此VWAP = sum(price_per_unit * quantity) / sum(quantity)

【问题讨论】:

  • 听起来像cumulative sum aggregation 会有所帮助。
  • 你能想出一个代码示例来解决上面的问题吗?我尝试查看累积总和,但无法真正实现。
  • 我提供的链接中有一个示例。而且我无法提供比那个更好的样本,因为您没有提供任何测试数据、所需的输出和索引映射。
  • @AndreiStefan 我已经用相关映射更新了问题,price_per_unitquantity 只是数字,所以不需要真实的样本数据;感谢您的帮助,非常感谢,因为一开始很难理解 ES 的聚合。

标签: elasticsearch elasticsearch-aggregation elasticsearch-query


【解决方案1】:
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

【讨论】:

  • 谢谢,这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-09-01
  • 2020-08-10
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多