# Aggregate sum of first child

**URL:** <https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258>\
**Category:** Elasticsearch\
**Created:** [June 29, 2017, 9:27am UTC](https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258 "2017-06-29T09:27:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silas\_Hansen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silas_hansen/32/67521_2.png) [@Silas\_Hansen](https://discuss.elastic.co/u/Silas_Hansen)\
**Post date:** [June 29, 2017, 9:27am UTC](https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258/1 "2017-06-29T09:27:40Z")

</div>

I have a parent-child relation where I want to do an aggregation on the parent, that sums up an integer property on the child items. The issue is that I have multiple children for each parent, and I am only interested in the first one for this particular sum.

Can this be done in pure ES aggregation fashion?

Using orders and orderlines as an example:

Order1  
&nbsp;&nbsp;&nbsp;&nbsp;- OrderLine1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - price: 100  
&nbsp;&nbsp;&nbsp;&nbsp;- OrderLine2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - price: 120

Order2  
&nbsp;&nbsp;&nbsp;&nbsp;- OrderLine1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - price: 523  
&nbsp;&nbsp;&nbsp;&nbsp;- OrderLine2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - price: 220  
&nbsp;&nbsp;&nbsp;&nbsp;- OrderLine3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - price: 300

I would like to get the sum of price for all orders, but only taking the first child for each parent, so I would expect to get a sum of 100+523. It seems that the top\_hits aggregation is not useful in this regard, but I could just be using it wrong. I do have other properties to sort on, if "first" needs to be relative to something.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [June 29, 2017, 1:45pm UTC](https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258/2 "2017-06-29T13:45:22Z")

</div>

you can only number orderline by a dedicated field and then filter children for the aggregation.

---

<div class="post-metadata">

**Author:** ![Silas\_Hansen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silas_hansen/32/67521_2.png) [@Silas\_Hansen](https://discuss.elastic.co/u/Silas_Hansen)\
**Post date:** [June 29, 2017, 1:47pm UTC](https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258/3 "2017-06-29T13:47:49Z")

</div>

Okay, that was also the solution that I had in mind, but it requires me to re-index and change a whole lot of systems writing the data, so I wanted to see if I could come up with another solution before going down that path.

Thank you for taking the time to answer.

---

<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 27, 2017, 1:48pm UTC](https://discuss.elastic.co/t/aggregate-sum-of-first-child/91258/4 "2017-07-27T13:48:00Z")

</div>

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