请问elaelasticsearch怎么实现单价*数量,再求和。
普通的sum我会,但是每条数据先要单价*数量,然后再求和,这样的,请问怎么实现。
字段:
quantity_ordered (数量)
item_price ( 单价)
普通的sum我会,但是每条数据先要单价*数量,然后再求和,这样的,请问怎么实现。
字段:
quantity_ordered (数量)
item_price ( 单价)
3 个回复
Charele - Cisco4321
赞同来自: goodboy_haidao 、Tjian
"size":0,
"aggs" : {
"aggs1" : {
"sum" : {
"script" : "doc['x'].value * doc['y'].value"
}
}
}
}
goodboy_haidao
赞同来自:
"total":{
"scripted_metric":{
"init_script":"state.transactions = []",
"map_script":"state.transactions.add(doc.quantity_ordered.value * doc.item_price.value)",
"combine_script":"double total = 0; for (t in state.transactions) { total += t } return total",
"reduce_script":"double total = 0; for (a in states) { if (null == a) continue; total += a} return total"
}
},
这样的又长,又麻烦!
God_lockin
赞同来自: