从sql转换为elasticsearch查询

时间:2015-03-23 04:20:50

标签: mysql sql elasticsearch aggregation

如何将以下sql查询转换为弹性搜索查询?

SELECT sum(`price_per_unit`*`quantity`) as orders 
FROM       `order_demormalize` 
WHERE date(`order_date`)='2014-04-15'

1 个答案:

答案 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"
         }
      }
   }
}