# Aggregations: sort by mutliple terms

**URL:** <https://discuss.elastic.co/t/aggregations-sort-by-mutliple-terms/265081>\
**Category:** Elasticsearch\
**Created:** [February 22, 2021, 2:38pm UTC](https://discuss.elastic.co/t/aggregations-sort-by-mutliple-terms/265081 "2021-02-22T14:38:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Douglas\_Leister](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/douglas_leister/32/50610_2.png) [@Douglas\_Leister](https://discuss.elastic.co/u/Douglas_Leister)\
**Post date:** [February 22, 2021, 2:38pm UTC](https://discuss.elastic.co/t/aggregations-sort-by-mutliple-terms/265081/1 "2021-02-22T14:38:04Z")

</div>

I have a mapping of businesses, for which I am using an aggregation to calculate average ratings for the businesses grouped by business id. Sorting the aggregation by business id or average rating works fine. My issue is that I would like to have the ability to also sort by business name. I have tried to create a sub aggregation on id to create a name bucket and sort by that. But I understand that I can only sort by a single bucket or metric aggregation value.

**Mapping**

```auto
{
    "properties": {
        "id": {"type": "keyword"},
        "name": {
            "type": "text",
            "fields": {
                "kw": {"type": "keyword"}
            }
        },
        "reviews": {
            "type": "nested",
            "properties": {
                "id": {"type": "keyword"},
                "rating": {"type": "float"}
            }
        }
    }
}

```

**Search**

```auto
{
    "query": {
        "match": {
            "name": {
                "query": "plumbing"
            }
        }
    },
    "aggs": {
        "stats": {
            "terms": {
                "field": "id",
                "size": 2,
                "order": {"name": "desc"}
            },
            "aggs": {
                "reviews": {
                    "nested": {
                        "path": "reviews"
                    },
                    "aggs": {
                        "average": {
                            "avg": {"field": "reviews.rating"}
                        }
                    }
                },
                "name": {
                    "terms": {
                        "field": "name.kw"
                    }
                }
            }
        }
    }

```

When I use the below search on this mapping, I get the following error:

```auto
Error: aggregation_execution_exception
Reason: Invalid aggregation order path [name]. 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.

```

---

<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:** [March 22, 2021, 2:38pm UTC](https://discuss.elastic.co/t/aggregations-sort-by-mutliple-terms/265081/2 "2021-03-22T14:38:49Z")

</div>

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