# Multi-Dimensionality

**URL:** <https://discuss.elastic.co/t/multi-dimensionality/29218>\
**Category:** Elasticsearch\
**Created:** [September 14, 2015, 8:03am UTC](https://discuss.elastic.co/t/multi-dimensionality/29218 "2015-09-14T08:03:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthik\_kurella](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karthik_kurella/32/63253_2.png) [@karthik\_kurella](https://discuss.elastic.co/u/karthik_kurella)\
**Post date:** [September 14, 2015, 8:03am UTC](https://discuss.elastic.co/t/multi-dimensionality/29218/1 "2015-09-14T08:03:04Z")

</div>

How to query an elastic search to get two dimensional table as output?  
Example:  
Students as columns, Subjects as rows and marks as values in Student Subject table.

---

<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:** [September 14, 2015, 8:40am UTC](https://discuss.elastic.co/t/multi-dimensionality/29218/2 "2015-09-14T08:40:00Z")

</div>

Aggregations provide the ability to summarise data in different categories.

It is useful to think about a tree structure rather than to talk of tables. The `terms` aggregation can organise documents into buckets, each of which can be further subdivided by other aggregations into further sub-buckets. Your query would look something like this:

```
POST test/result
{
	"student":"albert",
	"subject":"physics",
	"score":99
}
POST test/result
{
	"student":"benoit",
	"subject":"mathematics",
	"score":97
}
POST test/result
{
	"student":"joe",
	"subject":"mathematics",
	"score":33
}
GET test/result/_search
{
	"size":0,
	"aggregations":{
		"subjects":{
			"terms":{
				"field":"subject"
			},
			"aggregations":{
				"students":{
					"terms":{
						"field":"student"
					},
					"aggregations":{
						"max_score":{
							"max":{
								"field":"score"
							}
						}
					}
				}
			}
		}
	}
}

```

This last query returns a tree structure like this:

```
{
   "took": 29,
   "timed_out": false,
   "_shards": {
	  "total": 5,
	  "successful": 5,
	  "failed": 0
   },
   "hits": {
	  "total": 3,
	  "max_score": 0,
	  "hits": []
   },
   "aggregations": {
	  "subjects": {
		 "doc_count_error_upper_bound": 0,
		 "sum_other_doc_count": 0,
		 "buckets": [
			{
			   "key": "mathematics",
			   "doc_count": 2,
			   "students": {
				  "doc_count_error_upper_bound": 0,
				  "sum_other_doc_count": 0,
				  "buckets": [
					 {
						"key": "benoit",
						"doc_count": 1,
						"max_score": {
						   "value": 97
						}
					 },
					 {
						"key": "joe",
						"doc_count": 1,
						"max_score": {
						   "value": 33
						}
					 }
				  ]
			   }
			},
			{
			   "key": "physics",
			   "doc_count": 1,
			   "students": {
				  "doc_count_error_upper_bound": 0,
				  "sum_other_doc_count": 0,
				  "buckets": [
					 {
						"key": "albert",
						"doc_count": 1,
						"max_score": {
						   "value": 99
						}
					 }
				  ]
			   }
			}
		 ]
	  }
   }
}
```

---

<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:** [July 5, 2017, 11:50pm UTC](https://discuss.elastic.co/t/multi-dimensionality/29218/3 "2017-07-05T23:50:32Z")

</div>

Closed on July 5, 2017, 11:50pm UTC


