# Ordering nested aggregation

**URL:** <https://discuss.elastic.co/t/ordering-nested-aggregation/44212>\
**Category:** Elasticsearch\
**Created:** [March 11, 2016, 11:14pm UTC](https://discuss.elastic.co/t/ordering-nested-aggregation/44212 "2016-03-11T23:14:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruben\_van\_Vreeland](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruben_van_vreeland/32/8443_2.png) [@Ruben\_van\_Vreeland](https://discuss.elastic.co/u/Ruben_van_Vreeland)\
**Post date:** [March 11, 2016, 11:14pm UTC](https://discuss.elastic.co/t/ordering-nested-aggregation/44212/1 "2016-03-11T23:14:18Z")

</div>

When executing a aggregation using nested objects, I get the following error when sorting on a metric:  
**Sub-path [nested] points to non single-bucket aggregation**.

According to [ElasticSearch 2.2 NestedAggregation](https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java), a nested aggregation actually is [single-bucket](https://github.com/elastic/elasticsearch/blob/2.2/core/src/main/java/org/elasticsearch/search/aggregations/bucket/SingleBucketAggregator.java).

Source for aggregation validation that is throwing this error can be found at:  
[aggregations/support/AggregationPath.java](https://github.com/elastic/elasticsearch/blob/2.2/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationPath.java)

**Query**

```
{
  "aggregations": {
    "titleGroup": {
      "terms": {
        "field": "documenTitle",
        "order": {
          "nested > cardinality": "desc"
        }
      },
      "aggregations": {
        "nested": {
          "nested": {
            "path": "nested_object"
          },
          "aggregations": {
            "cardinality": {
              "cardinality": {
                "field": "nested_object.hash"
              }
            }
}}}}}}

```

**Error**

```
{
  "error": {
    "root_cause": [
      {
        "type": "aggregation_execution_exception",
        "reason": "Invalid terms aggregation order path [nested>cardinality]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. Sub-path [nested] points to non single-bucket aggregation"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "bitsensor",
        "node": "7uApA39rQOGMX4soKZQung",
        "reason": {
          "type": "aggregation_execution_exception",
          "reason": "Invalid terms aggregation order path [nested>cardinality]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. Sub-path [nested] points to non single-bucket aggregation"
        }
      }
    ]
  },
  "status": 500
}

```

**Mapping**

```
{
  "properties": {
    "documenTitle": {"type":"string"},
    "nested_object": {
      "type": "nested",
      "properties": {
        "hash": {
          "type": "long"
}}}}}
```

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [March 15, 2016, 9:33am UTC](https://discuss.elastic.co/t/ordering-nested-aggregation/44212/2 "2016-03-15T09:33:29Z")

</div>

@Ruben_van_Vreeland indeed this is a bug, see [https://github.com/elastic/elasticsearch/issues/16838#issuecomment-190210463](https://github.com/elastic/elasticsearch/issues/16838#issuecomment-190210463) for more information on this. You can also track that issue on Github.

---

<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 5, 2017, 11:08pm UTC](https://discuss.elastic.co/t/ordering-nested-aggregation/44212/3 "2017-07-05T23:08:25Z")

</div>


