# Sub agg depending on length of results

**URL:** <https://discuss.elastic.co/t/sub-agg-depending-on-length-of-results/129662>\
**Category:** Elasticsearch\
**Created:** [April 26, 2018, 12:35pm UTC](https://discuss.elastic.co/t/sub-agg-depending-on-length-of-results/129662 "2018-04-26T12:35:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [April 26, 2018, 12:35pm UTC](https://discuss.elastic.co/t/sub-agg-depending-on-length-of-results/129662/1 "2018-04-26T12:35:39Z")

</div>

Hello. I have several fields, and I want to put each one as a sub agg of another one.

Say A-\>subAgg(B)-\>subAgg(C)-\>subAgg(D)

But I want them to be put depending on their number of results. If for example D has the most results as an aggregation I want it to be put at first, and the next one is the one containing the second most results, say A... etc like so D-\>subAgg(A)-\>subAgg(B)-\>subAgg(C)

How can I do that?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 4, 2018, 3:06pm UTC](https://discuss.elastic.co/t/sub-agg-depending-on-length-of-results/129662/2 "2018-05-04T15:06:41Z")

</div>

Afraid that isn't possible. Order of the aggregation in the tree determines which and how documents match the child sub-aggregations. We can't reorder the aggs predominantly for technical reasons, but also because it would break the counts and metrics. The generated counts/metrics are only valid in the order specified.

What are you trying to accomplish?

---

<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:** [June 1, 2018, 3:06pm UTC](https://discuss.elastic.co/t/sub-agg-depending-on-length-of-results/129662/3 "2018-06-01T15:06:43Z")

</div>

Closed on June 1, 2018, 3:06pm UTC

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