# Can elasticsearch do GROUP BY multi fields and ORDER BY count?

**URL:** <https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128>\
**Category:** Elasticsearch\
**Created:** [April 10, 2019, 2:47am UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128 "2019-04-10T02:47:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rookie1](https://avatars.discourse-cdn.com/v4/letter/r/ea5d25/32.png) [@rookie1](https://discuss.elastic.co/u/rookie1)\
**Post date:** [April 10, 2019, 2:47am UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128/1 "2019-04-10T02:47:07Z")

</div>

In SQL I would do it possibly like this:

```auto
SELECT field1,field2, count(*) as cnt FROM table GROUP BY field1,field2 ORDER BY cnt DESC;

```

Is aggregate query like that possible with ES?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 10, 2019, 9:29am UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128/2 "2019-04-10T09:29:29Z")

</div>

Hi rookie1.  
Yes, you can group data by multiple fields. One difference from SQL is that that results can be a tree structure with hierarchy rather than thinking of them like a flattened table of results.  
You would use the `terms` aggregation to group information. For example, given an index of investment data field1 might be investor and field 2 might be the company invested in:

```
GET /crunchbase/_search
{
  "size":0,
  "aggregations": {
	"first_by_investor": {
	  "terms": {
		"field": "investor"
	  },
	  "aggregations": {
		"then_by_company": {
		  "terms": {
			"field": "company"
		  }
		}
	  }
	}
  }
}

```

The results are a hierarchy like this (default sort size is by number of docs):

```
...	
  "aggregations" : {
	"first_by_investor" : {
	  "buckets" : [
		{
		  "key" : "New Enterprise Associates",
		  "doc_count" : 445,
		  "then_by_company" : {
			"buckets" : [
			  {
				"key" : "PatientKeeper",
				"doc_count" : 5
			  },
			  {
				"key" : "SolFocus",
				"doc_count" : 5
			  }
              ...
			},
			{
			  "key" : "SV Angel",
			  "doc_count" : 436,				  
			   ...
			}
    ]
```

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [April 11, 2019, 7:16am UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128/3 "2019-04-11T07:16:40Z")

</div>

@rookie1 or you can try exactly the same query you have there in [Elasticsearch SQL](https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.html) and the results will be displayed just like it would when using a relational database.

Or you can use the [ES SQL translate API](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate.html) to see what kind of Elastisearch DSL query we create from the SQL query provided. Please, note that the query will be slightly different from the one @Mark_Harwood provided, because ES SQL will use a `composite` aggregation on top to allow users to paginate through the results (a common requirement in SQL world using cursors).

---

<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:** [May 9, 2019, 7:16am UTC](https://discuss.elastic.co/t/can-elasticsearch-do-group-by-multi-fields-and-order-by-count/176128/4 "2019-05-09T07:16:57Z")

</div>

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