Aggregate sum of first child

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:

    - OrderLine1
         - price: 100
    - OrderLine2
         - price: 120

    - OrderLine1
         - price: 523
    - OrderLine2
         - price: 220
    - OrderLine3
         - 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!

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

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.

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