0

How can i convert the following sql query into elastic search query?

SELECT sum(`price_per_unit`*`quantity`) as orders 
FROM       `order_demormalize` 
WHERE date(`order_date`)='2014-04-15'
1
  • Cleaned up formatting Commented Mar 23, 2015 at 5:35

1 Answer 1

1

You need to use scripts to compute the product of values. For newer versions of Elasticsearch, enable dynamic scripting by adding the line script.disable_dynamic: false in elasticsearch.yml file. Note that this may leave a security hole in your Elasticsearch cluster. So enable scripting judiciously. Try the query below:

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"
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am not able to access script ie doc['price_per_unit'].value .
Is there any other way to do this?
What version of Elasticsearch are you using?
I am using elasticsearch - 1.4.4
Then you need to enable scripting by adding the line script.disable_dynamic: false in elasticsearch.yml file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.