如何将以下sql查询转换为弹性搜索查询?
SELECT sum(`price_per_unit`*`quantity`) as orders
FROM `order_demormalize`
WHERE date(`order_date`)='2014-04-15'
答案 0 :(得分:1)
您需要使用脚本来计算值的乘积。对于较新版本的Elasticsearch,请在script.disable_dynamic: false
文件中添加行elasticsearch.yml
以启用动态脚本。请注意,这可能会在Elasticsearch集群中留下安全漏洞。因此,明智地启用脚本。请尝试以下查询:
POST <indexname>/<typename>/_search?search_type=count
{
"query": {
"filtered": {
"filter": {
"term": {
"order_date": "2014-04-15"
}
}
}
},
"aggs": {
"orders": {
"sum": {
"script": "doc['price_per_unit'].value * doc['quantity'].value"
}
}
}
}