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。
听起来像[累积总和聚合](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html)会有所帮助。 –
你能想出一个来自上面问题的代码示例吗?我试图查看累计总和,但无法真正实现它。 –
我提供的链接中有一个示例。由于您未提供任何测试数据,期望的输出和索引映射,因此我无法提供比此更好的示例。 –