How to write a efficient ES query for the below SQL query?
Select sum(visits) visits
from index
group by ip, port
order by visits desc
limit 10
I am using below ES query, but this will order the visit inside port bucket. I wan't to get top visits after grouping them by destination ip and port.
"aggregations" : {
"ip":
{
"terms":
{
"field": "ip",
"size": 10
},
"aggregations":
{
"port":
{
"terms":
{
"field": "port",
"size": 0,
"order":
{
"visits": "desc"
}
},
"aggregations":
{
"visits":
{
"sum":
{
"field": "visits"
}
}
}
}
}
}
}
Can some one help me with this?
Thanks.