# Aggregation on parent/child documents

**URL:** <https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902>\
**Category:** Elasticsearch\
**Created:** [July 25, 2014, 11:54am UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902 "2014-07-25T11:54:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [July 25, 2014, 11:54am UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902/1 "2014-07-25T11:54:50Z")

</div>

Hi,

I wanted to ask whether is possible to perform aggregations combining  
parent/child documents, something similar with the nested aggregation and  
the reverse nested aggregation. It would be very helpful to have the  
ability to create for instance buckets based on parent document fields and  
get back aggregations that contain fields of both parent and children  
documents combined.

Any thoughts, future features to be added in the near releases, related to  
the above?

Thank you  
Thomas

--  
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/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 25, 2014, 5:12pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902/2 "2014-07-25T17:12:41Z")

</div>

Hi Thomas,

None of the aggregations that we have today can leverage parent/child  
relations. However, there is a `children` aggregation in the pipeline:

> <https://github.com/elastic/elasticsearch/pull/6936>
>
> Add \`children\` bucket aggregation that is able to map buckets between parent typ…es and child types based on top of the parent/child support. It is the equivalent of the has\_child filter/query in the query dsl.
> 
> Example request:
> 
> \`\`\` json
> GET /stack/question/\_search?search\_type=count
> {
> "aggs": {
> "top-tags": {
> "terms": {
> "field": "tags",
> "size": 10
> },
> "aggs": {
> "to-answers": {
> "children": {
> "child\_type" : "answer"
> },
> "aggs": {
> "top-names": {
> "terms": {
> "field": "owner\_display\_name",
> "size": 10
> }
> }
> }
> }
> }
> }
> }
> }
> \`\`\`
> 
> Example response:
> 
> \`\`\` json
> {
> "took": 90,
> "timed\_out": false,
> "\_shards": {
> "total": 5,
> "successful": 5,
> "failed": 0
> },
> "hits": {
> "total": 175275,
> "max\_score": 0,
> "hits": \[\]
> },
> "aggregations": {
> "top-tags": {
> "buckets": \[
> {
> "key": "windows-7",
> "doc\_count": 25365,
> "to-answers": {
> "doc\_count": 36004,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 274
> },
> {
> "key": "chris",
> "doc\_count": 19
> },
> {
> "key": "david",
> "doc\_count": 14
> },
> {
> "key": "dan",
> "doc\_count": 12
> },
> {
> "key": "james",
> "doc\_count": 7
> },
> {
> "key": "b",
> "doc\_count": 5
> },
> {
> "key": "alex",
> "doc\_count": 4
> },
> {
> "key": "peter",
> "doc\_count": 4
> },
> {
> "key": "s",
> "doc\_count": 4
> },
> {
> "key": "cody",
> "doc\_count": 3
> }
> \]
> }
> }
> },
> {
> "key": "linux",
> "doc\_count": 18342,
> "to-answers": {
> "doc\_count": 6655,
> "top-names": {
> "buckets": \[
> {
> "key": "abrams",
> "doc\_count": 25
> },
> {
> "key": "ignacio",
> "doc\_count": 25
> },
> {
> "key": "vazquez",
> "doc\_count": 25
> },
> {
> "key": "chris",
> "doc\_count": 9
> },
> {
> "key": "michael",
> "doc\_count": 7
> },
> {
> "key": "basile",
> "doc\_count": 6
> },
> {
> "key": "david",
> "doc\_count": 6
> },
> {
> "key": "alex",
> "doc\_count": 4
> },
> {
> "key": "botykai",
> "doc\_count": 3
> },
> {
> "key": "paul",
> "doc\_count": 3
> }
> \]
> }
> }
> },
> {
> "key": "windows",
> "doc\_count": 18119,
> "to-answers": {
> "doc\_count": 24051,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 265
> },
> {
> "key": "david",
> "doc\_count": 27
> },
> {
> "key": "chris",
> "doc\_count": 26
> },
> {
> "key": "diago",
> "doc\_count": 9
> },
> {
> "key": "john",
> "doc\_count": 7
> },
> {
> "key": "paxdiablo",
> "doc\_count": 7
> },
> {
> "key": "ben",
> "doc\_count": 6
> },
> {
> "key": "mark",
> "doc\_count": 6
> },
> {
> "key": "adam",
> "doc\_count": 5
> },
> {
> "key": "c",
> "doc\_count": 5
> }
> \]
> }
> }
> },
> {
> "key": "osx",
> "doc\_count": 10971,
> "to-answers": {
> "doc\_count": 5902,
> "top-names": {
> "buckets": \[
> {
> "key": "diago",
> "doc\_count": 4
> },
> {
> "key": "albert",
> "doc\_count": 3
> },
> {
> "key": "asmus",
> "doc\_count": 3
> },
> {
> "key": "molly7244",
> "doc\_count": 3
> },
> {
> "key": "aaron",
> "doc\_count": 2
> },
> {
> "key": "abizern",
> "doc\_count": 2
> },
> {
> "key": "adam",
> "doc\_count": 2
> },
> {
> "key": "duskwuff",
> "doc\_count": 2
> },
> {
> "key": "johnsyweb",
> "doc\_count": 2
> },
> {
> "key": "mark",
> "doc\_count": 2
> }
> \]
> }
> }
> },
> {
> "key": "ubuntu",
> "doc\_count": 8743,
> "to-answers": {
> "doc\_count": 8784,
> "top-names": {
> "buckets": \[
> {
> "key": "ignacio",
> "doc\_count": 9
> },
> {
> "key": "abrams",
> "doc\_count": 8
> },
> {
> "key": "molly7244",
> "doc\_count": 8
> },
> {
> "key": "david",
> "doc\_count": 7
> },
> {
> "key": "pate",
> "doc\_count": 6
> },
> {
> "key": "roger",
> "doc\_count": 6
> },
> {
> "key": "chris",
> "doc\_count": 5
> },
> {
> "key": "vazquez",
> "doc\_count": 5
> },
> {
> "key": "paul",
> "doc\_count": 3
> },
> {
> "key": "rob",
> "doc\_count": 3
> }
> \]
> }
> }
> },
> {
> "key": "windows-xp",
> "doc\_count": 7517,
> "to-answers": {
> "doc\_count": 13610,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 232
> },
> {
> "key": "chris",
> "doc\_count": 9
> },
> {
> "key": "john",
> "doc\_count": 9
> },
> {
> "key": "david",
> "doc\_count": 8
> },
> {
> "key": "dave",
> "doc\_count": 5
> },
> {
> "key": "b",
> "doc\_count": 4
> },
> {
> "key": "bart",
> "doc\_count": 4
> },
> {
> "key": "joeqwerty",
> "doc\_count": 4
> },
> {
> "key": "mike",
> "doc\_count": 4
> },
> {
> "key": "s",
> "doc\_count": 4
> }
> \]
> }
> }
> },
> {
> "key": "networking",
> "doc\_count": 6739,
> "to-answers": {
> "doc\_count": 2076,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 6
> },
> {
> "key": "alnitak",
> "doc\_count": 5
> },
> {
> "key": "chris",
> "doc\_count": 3
> },
> {
> "key": "albin",
> "doc\_count": 2
> },
> {
> "key": "brian",
> "doc\_count": 2
> },
> {
> "key": "everett",
> "doc\_count": 2
> },
> {
> "key": "fishdump",
> "doc\_count": 2
> },
> {
> "key": "m",
> "doc\_count": 2
> },
> {
> "key": "mike",
> "doc\_count": 2
> },
> {
> "key": "p",
> "doc\_count": 2
> }
> \]
> }
> }
> },
> {
> "key": "mac",
> "doc\_count": 5590,
> "to-answers": {
> "doc\_count": 999,
> "top-names": {
> "buckets": \[
> {
> "key": "abrams",
> "doc\_count": 2
> },
> {
> "key": "ignacio",
> "doc\_count": 2
> },
> {
> "key": "vazquez",
> "doc\_count": 2
> },
> {
> "key": "adam",
> "doc\_count": 1
> },
> {
> "key": "anon",
> "doc\_count": 1
> },
> {
> "key": "aravindhanarvi",
> "doc\_count": 1
> },
> {
> "key": "arkaaito",
> "doc\_count": 1
> },
> {
> "key": "ballard",
> "doc\_count": 1
> },
> {
> "key": "bart",
> "doc\_count": 1
> },
> {
> "key": "ben",
> "doc\_count": 1
> }
> \]
> }
> }
> },
> {
> "key": "wireless-networking",
> "doc\_count": 4409,
> "to-answers": {
> "doc\_count": 6497,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 61
> },
> {
> "key": "chris",
> "doc\_count": 5
> },
> {
> "key": "mike",
> "doc\_count": 5
> },
> {
> "key": "tom",
> "doc\_count": 5
> },
> {
> "key": "bart",
> "doc\_count": 4
> },
> {
> "key": "user48838",
> "doc\_count": 4
> },
> {
> "key": "alex",
> "doc\_count": 2
> },
> {
> "key": "anon31097",
> "doc\_count": 2
> },
> {
> "key": "joeqwerty",
> "doc\_count": 2
> },
> {
> "key": "kevin",
> "doc\_count": 2
> }
> \]
> }
> }
> },
> {
> "key": "windows-8",
> "doc\_count": 3601,
> "to-answers": {
> "doc\_count": 4263,
> "top-names": {
> "buckets": \[
> {
> "key": "molly7244",
> "doc\_count": 3
> },
> {
> "key": "msft",
> "doc\_count": 2
> },
> {
> "key": "user172132",
> "doc\_count": 2
> },
> {
> "key": "aj",
> "doc\_count": 1
> },
> {
> "key": "algorithms",
> "doc\_count": 1
> },
> {
> "key": "andersson",
> "doc\_count": 1
> },
> {
> "key": "ashafiee",
> "doc\_count": 1
> },
> {
> "key": "balakrishnan",
> "doc\_count": 1
> },
> {
> "key": "bobby",
> "doc\_count": 1
> },
> {
> "key": "brockschmidt",
> "doc\_count": 1
> }
> \]
> }
> }
> }
> \]
> }
> }
> }
> \`\`\`

On Fri, Jul 25, 2014 at 1:54 PM, Thomas [thomas.bolis@gmail.com](mailto:thomas.bolis@gmail.com) wrote:

> Hi,
> 
> I wanted to ask whether is possible to perform aggregations combining  
> parent/child documents, something similar with the nested aggregation and  
> the reverse nested aggregation. It would be very helpful to have the  
> ability to create for instance buckets based on parent document fields and  
> get back aggregations that contain fields of both parent and children  
> documents combined.
> 
> Any thoughts, future features to be added in the near releases, related to  
> the above?
> 
> Thank you  
> Thomas
> 
> --  
> 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/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/91d60d52-c538-45b5-8cf0-91cb1e9d9a9a%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j6s3BM-fS-LdZU0hfdBBwAYBaVGpi3j95xzhBGsckrpgg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6s3BM-fS-LdZU0hfdBBwAYBaVGpi3j95xzhBGsckrpgg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [July 25, 2014, 5:29pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902/3 "2014-07-25T17:29:58Z")

</div>

Hi Adrien and thank you for the reply,

This is exactly what i had in mind alongside with the reversed search  
equivalent with the reverse\_nested, this is planed for version 1.4.0  
onwards as i see, will keep track of any updates on this, thanks

Thomas

On Friday, 25 July 2014 14:54:50 UTC+3, Thomas wrote:

> Hi,
> 
> I wanted to ask whether is possible to perform aggregations combining  
> parent/child documents, something similar with the nested aggregation and  
> the reverse nested aggregation. It would be very helpful to have the  
> ability to create for instance buckets based on parent document fields and  
> get back aggregations that contain fields of both parent and children  
> documents combined.
> 
> Any thoughts, future features to be added in the near releases, related to  
> the above?
> 
> Thank you  
> Thomas

--  
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/a6c7dfa1-d8b1-4ce5-8046-73892f74b33e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a6c7dfa1-d8b1-4ce5-8046-73892f74b33e%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:13am UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/18902/4 "2017-07-06T01:13:02Z")

</div>


