# Sort on nested aggreagations

**URL:** <https://discuss.elastic.co/t/sort-on-nested-aggreagations/137416>\
**Category:** Elasticsearch\
**Created:** [June 26, 2018, 10:30am UTC](https://discuss.elastic.co/t/sort-on-nested-aggreagations/137416 "2018-06-26T10:30:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![magdalena179](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@magdalena179](https://discuss.elastic.co/u/magdalena179)\
**Post date:** [June 26, 2018, 10:30am UTC](https://discuss.elastic.co/t/sort-on-nested-aggreagations/137416/1 "2018-06-26T10:30:03Z")

</div>

I have nested mapping and doing nested aggregations on it. I would like to sort by the most inner value - sum\_num\_transactions in this case. How can I achieve it? I have expected that on .nested(name, "breakdown") will be available something like:  
`.order(BucketOrder.aggregation("sum_num_transactions", true))`  
but it is not there for nested (only for terms) . How can I sort on sum\_num\_transactions?

```
  private NestedAggregationBuilder buildNestedAggregations(String name) {
            return AggregationBuilders.nested(name, "breakdown")
                    .subAggregation(
                            AggregationBuilders
                                    .sum("sum_num_transactions").field("breakdown.num_transactions")
                    )
                   .subAggregation(
                            AggregationBuilders
                                  .sum("sum_commission_amount_in_cents").field("breakdown.commission_amount_in_cents")
                    );
        }
```

---

<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 24, 2018, 10:30am UTC](https://discuss.elastic.co/t/sort-on-nested-aggreagations/137416/2 "2018-07-24T10:30:04Z")

</div>

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