# Aggregation query on nested objects

**URL:** <https://discuss.elastic.co/t/aggregation-query-on-nested-objects/99961>\
**Category:** Elasticsearch\
**Created:** [September 10, 2017, 5:18am UTC](https://discuss.elastic.co/t/aggregation-query-on-nested-objects/99961 "2017-09-10T05:18:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sripathi\_kammari](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@sripathi\_kammari](https://discuss.elastic.co/u/sripathi_kammari)\
**Post date:** [September 10, 2017, 5:18am UTC](https://discuss.elastic.co/t/aggregation-query-on-nested-objects/99961/1 "2017-09-10T05:18:20Z")

</div>

Hello All,

I have the following json object

> ```
> {
> "responses": [
> {
> "question": {
> "name": "Please rate your experience"
> },
> "answers": [
> {
> "name": "4/10"
> }
> ]
> },
> {
> "question": {
> "name": "Designation"
> },
> "answers": [
> {
> "name": "Student"
> }
> ]
> },
> {
> "question": {
> "name": "Did you attempt any experiments ?"
> },
> "answers": [
> {
> "name": "No"
> }
> ]
> }
> ]
> }
> 
> ```

Here I would like to get the count of each unique answer to its corresponding question

I tried out following aggregation query

> ```
> {
> "aggs": {
> "questions": {
> "terms": {
> "field": "responses.question.name",
> "order" : { "_count" : "asc" },
> "size": 0
> },
> "aggs": {
> "answers": {
> "terms": {
> "field": "responses.answers.name"
> }
>                     
> }
>           
>             
> }
>                     
> }
>           
>             
> }
> }
> 
> ```

This is listing count of all the available answers for every questions as below

> ```
> {
> "took": 59,
> "timed_out": false,
> "_shards": {
> "total": 5,
> "successful": 5,
> "failed": 0
> },
> "hits": {
> "total": 1,
> "max_score": 1,
> "hits": [
> {
> "_index": "post_question",
> "_type": "feedback",
> "_id": "AV5qHAAXBWUeGnRiAgw2",
> "_score": 1,
> "_source": {
> "responses": [
> {
> "question": {
> "name": "Please rate your experience"
> },
> "answers": [
> {
> "name": "4/10"
> }
> ]
> },
> {
> "question": {
> "name": "Designation"
> },
> "answers": [
> {
> "name": "Student"
> }
> ]
> },
> {
> "question": {
> "name": "Did you attempt any experiments ?"
> },
> "answers": [
> {
> "name": "No"
> }
> ]
> }
> ]
> }
> }
> ]
> },
> "aggregations": {
> "questions": {
> "doc_count_error_upper_bound": 0,
> "sum_other_doc_count": 0,
> "buckets": [
> {
> "key": "Designation",
> "doc_count": 1,
> "answers": {
> "doc_count_error_upper_bound": 0,
> "sum_other_doc_count": 0,
> "buckets": [
> {
> "key": "4/10",
> "doc_count": 1
> },
> {
> "key": "No",
> "doc_count": 1
> },
> {
> "key": "Student",
> "doc_count": 1
> }
> ]
> }
> },
> {
> "key": "Did you attempt any experiments ?",
> "doc_count": 1,
> "answers": {
> "doc_count_error_upper_bound": 0,
> "sum_other_doc_count": 0,
> "buckets": [
> {
> "key": "4/10",
> "doc_count": 1
> },
> {
> "key": "No",
> "doc_count": 1
> },
> {
> "key": "Student",
> "doc_count": 1
> }
> ]
> }
> },
> {
> "key": "Please rate your experience",
> "doc_count": 1,
> "answers": {
> "doc_count_error_upper_bound": 0,
> "sum_other_doc_count": 0,
> "buckets": [
> {
> "key": "4/10",
> "doc_count": 1
> },
> {
> "key": "No",
> "doc_count": 1
> },
> {
> "key": "Student",
> "doc_count": 1
> }
> ]
> }
> }
> ]
> }
> }
> }
> 
> ```

Can somebody look into and help me out

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 11, 2017, 7:09am UTC](https://discuss.elastic.co/t/aggregation-query-on-nested-objects/99961/2 "2017-09-11T07:09:42Z")

</div>

Have a look at [nested objects](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/nested.html) and [nested aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-aggregations-bucket-nested-aggregation.html)

--Alex

---

<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:** [October 9, 2017, 7:46am UTC](https://discuss.elastic.co/t/aggregation-query-on-nested-objects/99961/3 "2017-10-09T07:46:29Z")

</div>

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