# Aggregation on nested type

**URL:** <https://discuss.elastic.co/t/aggregation-on-nested-type/17244>\
**Category:** Elasticsearch\
**Created:** [April 28, 2014, 11:07pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-type/17244 "2014-04-28T23:07:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![qvin\_net](https://avatars.discourse-cdn.com/v4/letter/q/7993a0/32.png) [@qvin\_net](https://discuss.elastic.co/u/qvin_net)\
**Post date:** [April 28, 2014, 11:07pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-type/17244/1 "2014-04-28T23:07:20Z")

</div>

Hello,  
I am new to elasticsearch and I am running into issues doing aggregations  
on nested documents. I have an index that uses nested types. The code I  
used to set up my test data set is given here

- [https://gist.github.com/qvin/9911e904c1d127c28dc7](https://gist.github.com/qvin/9911e904c1d127c28dc7)  
I want to perform bucket aggregations on the nested documents based on  
filters set on the top level documents properties. Here is the query (along  
with the result) that I came up with
- [https://gist.github.com/qvin/a40bd96d6df25d1b31c3](https://gist.github.com/qvin/a40bd96d6df25d1b31c3)  
In the output if you notice that the aggregated value is the same (1026)  
for all buckets. I am interested in aggregations per bucket. Please see the  
expected value listed in the gist. Can someone please help me figure this  
out? I also tried using the "nested" aggregation type as shown below but  
this throws an error.  
Any help is appreciated. I would like to solve this without making changes  
to the document schema if possible.  
Thanks!

curl -XGET '[http://localhost:9200/testdb/testtable/\_search](http://localhost:9200/testdb/testtable/_search) -d '{  
"size": 0,  
"aggs": {  
"report\_aggs": {  
"filter": {  
"and": [  
{  
"term": {  
"prop1": "1"  
}  
},  
{  
"term": {  
"prop2": "2"  
}  
},  
{  
"term": {  
"checkflag": "true"  
}  
}  
]  
},  
"aggs": {  
"report\_aggs\_nested": {  
"nested": {  
"path": "nesteddocs"  
},  
"aggs": {  
"aggs\_sum": {  
"terms": {  
"field": "nesteddocs.dockey"  
},  
"aggs": {  
"docvalsum": {  
"avg": {  
"field": "nesteddocs.docvalue"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}'

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/60646a0c-b3c8-404a-aa3f-34457a779ef6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/60646a0c-b3c8-404a-aa3f-34457a779ef6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:32am UTC](https://discuss.elastic.co/t/aggregation-on-nested-type/17244/2 "2017-07-06T01:32:58Z")

</div>


