# Can elasticsearch do GROUP BY and ORDER BY count?

**URL:** <https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365>\
**Category:** Elasticsearch\
**Created:** [November 8, 2016, 2:48pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365 "2016-11-08T14:48:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![linuxdev](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@linuxdev](https://discuss.elastic.co/u/linuxdev)\
**Post date:** [November 8, 2016, 2:48pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/1 "2016-11-08T14:48:16Z")

</div>

Let's say I have dictionary application where people come and look up word definitions.

Im trying to visualize top N most popular words during some timeframe.

In SQL I would do it possibly like this:

```auto
SELECT word, count(*) as cnt FROM lookups GROUP BY word ORDER BY cnt DESC;

```

So I plan on dumping data into ES in this format for example:

```
{"timestamp":"2016-11-07T12:38:56-05:00","word":"dog","is_mwu":"no","host":"08a2cf4807fb","ref":"dictionary"}
{"timestamp":"2016-11-07T12:38:58-05:00","word":"red","is_mwu":"no","host":"08a2cf4807fb","ref":"dictionary"}
{"timestamp":"2016-11-07T12:39:29-05:00","word":"red","is_mwu":"no","host":"08a2cf4807fb","ref":"dictionary"}
{"timestamp":"2016-11-08T09:05:49-05:00","word":"dog","is_mwu":"no","host":"08a2cf4807fb","ref":"dictionary"}

```

Is aggregate query like that possible with ES?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 8, 2016, 2:55pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/2 "2016-11-08T14:55:43Z")

</div>

You'll want to do a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html).

---

<div class="post-metadata">

**Author:** ![linuxdev](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@linuxdev](https://discuss.elastic.co/u/linuxdev)\
**Post date:** [November 8, 2016, 3:18pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/3 "2016-11-08T15:18:26Z")

</div>

thanks

---

<div class="post-metadata">

**Author:** ![linuxdev](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@linuxdev](https://discuss.elastic.co/u/linuxdev)\
**Post date:** [November 8, 2016, 6:46pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/4 "2016-11-08T18:46:44Z")

</div>

Would I be able to set up those terms aggregations in kibana using some off the shelf chart?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 8, 2016, 8:09pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/5 "2016-11-08T20:09:46Z")

</div>

Yes. On the x axis settings (assuming a line chart; other kinds of visualizations works similarly), split the buckets, choose terms aggregation and the field you want to group by. You may find [https://www.timroes.de/2015/02/07/kibana-4-tutorial-part-3-visualize/](https://www.timroes.de/2015/02/07/kibana-4-tutorial-part-3-visualize/) helpful.

---

<div class="post-metadata">

**Author:** ![linuxdev](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@linuxdev](https://discuss.elastic.co/u/linuxdev)\
**Post date:** [November 8, 2016, 8:52pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/6 "2016-11-08T20:52:07Z")

</div>

Fantastic. I was able to launch everything in docker in minutes and get nice visual.  
Thank u!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 6, 2016, 8:52pm UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-and-order-by-count/65365/7 "2016-12-06T20:52:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
