elasticsearch,计算两个字段的乘积。

每成交一笔生成一条记录,其中有两个字段,一个是单价(price),一个是成交量(amount),怎么计算出5天内成交总额?

阅读 7.1k
1 个回答
curl -XPOST 'localhost:9200/order/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "filtered": {
            "query": {"match_all": {}},
                "filter": {
                    "range": {
                        "date": {
                            "from": ...,
                            "to": ...
                        }
                    }
                }
            }
        }
    },
    "aggs": {
        "total": {
            "scripted_metric": {
                "init_script" : "params._agg.transactions = []",
                "map_script" : "params._agg.transactions.add(doc.price.value * doc.amount.value)",
                "combine_script" : "double total = 0; for (t in params._agg.transactions) { total += t } return total",
                "reduce_script" : "double total = 0; for (a in params._aggs) { total += a } return total"
            }
        }
    }
}
'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏