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"
}
}
}
}
}
}
并且您需要为 groovy 启用内联脚本。