# Sort single doc buckets by an aggregation on a nested collection

**URL:** <https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365>\
**Category:** Elasticsearch\
**Created:** [November 13, 2017, 10:29am UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365 "2017-11-13T10:29:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hetzer-fastec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hetzer-fastec/32/21809_2.png) [@hetzer-fastec](https://discuss.elastic.co/u/hetzer-fastec)\
**Post date:** [November 13, 2017, 10:29am UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365/1 "2017-11-13T10:29:45Z")

</div>

Hi everyone,  
I am working on data modeling restaurants. A restaurants can have employees (which is a nested array of Employee) and an employee has a profession.  
The idea behind the following query attempt is to return all restaurants which employ people in more than 3 distinct professions, sorted by the amount of distinct professions. Except for the sorting, the query runs perfectly fine, but I have trouble getting the sorting right.

Do you guys have any ideas? 🙂

```
{
  "size": 0,
  "sort": [],
  "query": {
    "match_all": {}
  },
  "aggs": {
    "auxilary_id_grouping": {
      "terms": {
        "field": "_uid",
        "order": {
          "nested_employees_position_cardinality>employees_position_cardinality": "asc"
        }
      },
      "aggs": {
        "nested_employees_position_cardinality": {
          "nested": {
            "path": "employees"
          },
          "aggs": {
            "employees_position_cardinality": {
              "cardinality": {
                "field": "employees.position"
              }
            }
          }
        },
        "main_selector_agg": {
          "bucket_selector": {
            "script": {
              "inline": "(params.employees_position_cardinality) > (3)"
            },
            "buckets_path": {
              "employees_position_cardinality": "nested_employees_position_cardinality>employees_position_cardinality"
            }
          }
        },
        "auxilary_actual_document": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![hetzer-fastec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hetzer-fastec/32/21809_2.png) [@hetzer-fastec](https://discuss.elastic.co/u/hetzer-fastec)\
**Post date:** [November 24, 2017, 4:52pm UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365/2 "2017-11-24T16:52:48Z")

</div>

Noone here who can help? 🙂

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 27, 2017, 3:19pm UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365/3 "2017-11-27T15:19:54Z")

</div>

It's currently not possible to sort on nested aggregations (see this GitHub issue: [https://github.com/elastic/elasticsearch/issues/16838](https://github.com/elastic/elasticsearch/issues/16838) )

There's a fix coming in 6.1 that should resolve this issue.

---

<div class="post-metadata">

**Author:** ![hetzer-fastec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hetzer-fastec/32/21809_2.png) [@hetzer-fastec](https://discuss.elastic.co/u/hetzer-fastec)\
**Post date:** [November 27, 2017, 3:53pm UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365/4 "2017-11-27T15:53:22Z")

</div>

Great, thanks a lot! 🙂

---

<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:** [December 25, 2017, 3:54pm UTC](https://discuss.elastic.co/t/sort-single-doc-buckets-by-an-aggregation-on-a-nested-collection/107365/5 "2017-12-25T15:54:04Z")

</div>

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