Elasticsearch grouping queries

Photo by Caspar Camille Rubin on Unsplash

Working with Elasticsearch and doing some analytics on data stored in that, sometimes I had to perform some queries that in SQL are very simple, like

SELECT fieldName, COUNT(*)
FROM table
WHERE fieldName = X
GROUP BY fieldName
ORDER BY COUNT(*) DESC

With Elasticsearch you could obtain same results in different ways, but the more simple I found is:

GET table/_search?size=N
{
"aggs":{
"distict_field":{
"cardinality": {
"field": "fieldName"
}
}
}
}

I hope this could help someone.

Originally published at https://gabriele-decapoa.github.io.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store