# Unable to perform group on one field and sort on another field in Terms aggregation

**URL:** <https://discuss.elastic.co/t/unable-to-perform-group-on-one-field-and-sort-on-another-field-in-terms-aggregation/123212>\
**Category:** Elasticsearch\
**Created:** [March 9, 2018, 7:39am UTC](https://discuss.elastic.co/t/unable-to-perform-group-on-one-field-and-sort-on-another-field-in-terms-aggregation/123212 "2018-03-09T07:39:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shobhit121](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@shobhit121](https://discuss.elastic.co/u/shobhit121)\
**Post date:** [March 9, 2018, 7:39am UTC](https://discuss.elastic.co/t/unable-to-perform-group-on-one-field-and-sort-on-another-field-in-terms-aggregation/123212/1 "2018-03-09T07:39:04Z")

</div>

We want to perform group on src\_id and sort on field alias1.Is there any possibility to perform this action.

Please find query below.

```
"aggs": {
    "grp_by_src": {
      "terms": {
        "field": "src_id",
        "order": {
          "alias1": "asc"
        }
      },
      "aggs": {
        "visitor_count": {
          "sum": {
            "field": "visitor_count",
            "missing": 0
          }
        }
      }
    },
    "sum_total": {
      "sum": {
        "field": "visitor_count",
        "missing": 0
      }
    }

```

}

---

<div class="post-metadata">

**Author:** ![dmitri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitri/32/17997_2.png) [@dmitri](https://discuss.elastic.co/u/dmitri)\
**Post date:** [March 16, 2018, 10:41am UTC](https://discuss.elastic.co/t/unable-to-perform-group-on-one-field-and-sort-on-another-field-in-terms-aggregation/123212/2 "2018-03-16T10:41:03Z")

</div>

Hi,

If I understand correctly, you want to sort the result aggregated buckets based on some other field, right? If that is the case, you can use the [bucket\_sort](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-pipeline-bucket-sort-aggregation.html) aggregation. Have a look at the docs for examples.

---

<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:** [April 13, 2018, 10:41am UTC](https://discuss.elastic.co/t/unable-to-perform-group-on-one-field-and-sort-on-another-field-in-terms-aggregation/123212/3 "2018-04-13T10:41:16Z")

</div>

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