I studied elasticsearch aggregation queries but couldn't find if it supports multiple aggregate function. In an other word, I wanna know if elasticsearch can generate the equivalent of this Sql aggregation query:
SELECT account_no, transaction_type, count(account_no), sum(amount), max(amount) FROM index_name GROUP BY account_no, transaction_type Having count(account_no) > 10
If yes, how? Thank you.
There are two possible ways to do what you are looking for in ES and I've mentioned them both below.
I've also added sample mapping and sample documents for your reference.
Mapping:
Sample Documents:
Notice carefully, I'm only creating list of 4 transactions for 1 customer.
There are two ways to get what you are looking for:
Solution 1: Using Elasticsearch Query DSL
Aggregation Query:
For Aggregation Query DSL, I've made use of the below aggregation queries to solve what you are looking for.
Below is how query is summarised version of the query so that you get the clarity on which queries are sibling and which are parents.
Below is the actual query:
Important thing to mention is
min_doc_countwhich is nothing but thehaving count(account_no)>10, which in my query I'm filtering only those transactions withhaving count(account_no) > 2Query Response
Notice the above result carefully, I've added comments wherever required so that it helps what part of sql query you are looking for.
Solution 2: Using Elasticsearch SQL(_xpack solution)
If you are making use of xpack feature of Elasticsearch's SQL Access, you can simply copy paste the SELECT Query as below for the mapping and document as mentioned above:
Elasticsearch SQL:
Elasticsearch SQL Result:
Note that I've tested the query in ES 6.5.4.
Hope this helps!