# Sort on term sub aggregation field

**URL:** <https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313>\
**Category:** Elasticsearch\
**Created:** [August 17, 2020, 8:14pm UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313 "2020-08-17T20:14:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imran1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imran1/32/74039_2.png) [@imran1](https://discuss.elastic.co/u/imran1)\
**Post date:** [August 17, 2020, 8:14pm UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313/1 "2020-08-17T20:14:23Z")

</div>

Suppose I have the following query:

```
{
  "aggs": {
    "distinct_part": {
      "terms": {
        "field": "part",
        "size": 1000
      }
    },
      "aggs": {
    "distinct_manufacturer": {
      "terms": {
        "field": "manufacturer",
        "size": 1000
      }
    }
  }
}

```

What is the easiest way to sort on **manufacturer** name?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [August 17, 2020, 8:38pm UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313/2 "2020-08-17T20:38:05Z")

</div>

Try

```auto
{
  "aggs": {
    "distinct_part": {
      "terms": {
        "field": "part",
        "size": 1000
      }
    },
    "aggs": {
      "distinct_manufacturer": {
        "terms": {
          "field": "manufacturer",
          "size": 1000,
          "order": {
            "_term": "asc"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![imran1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imran1/32/74039_2.png) [@imran1](https://discuss.elastic.co/u/imran1)\
**Post date:** [August 18, 2020, 2:40am UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313/3 "2020-08-18T02:40:10Z")

</div>

This is the confusion I have as well, it first sorts on part. Just to let you know my requirement, I want it to first sort on part and then on manufacturer. The order on the manufacturer sub aggregation doesn't seem to be working.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [August 18, 2020, 11:46am UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313/4 "2020-08-18T11:46:35Z")

</div>

Can you share your mapping and a small sample of the data?

---

<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:** [September 15, 2020, 11:46am UTC](https://discuss.elastic.co/t/sort-on-term-sub-aggregation-field/245313/5 "2020-09-15T11:46:39Z")

</div>

Closed on September 15, 2020, 11:46am UTC

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