# Sort aggregation buckets based on Keyword field/Scripted Non Metric Field

**URL:** <https://discuss.elastic.co/t/sort-aggregation-buckets-based-on-keyword-field-scripted-non-metric-field/240246>\
**Category:** Elasticsearch\
**Created:** [July 8, 2020, 2:04am UTC](https://discuss.elastic.co/t/sort-aggregation-buckets-based-on-keyword-field-scripted-non-metric-field/240246 "2020-07-08T02:04:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seetha93](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@Seetha93](https://discuss.elastic.co/u/Seetha93)\
**Post date:** [July 8, 2020, 2:04am UTC](https://discuss.elastic.co/t/sort-aggregation-buckets-based-on-keyword-field-scripted-non-metric-field/240246/1 "2020-07-08T02:04:26Z")

</div>

Hi I have docs with string and numeric fields. And I do aggs in the following manner:

```
{
  "from": 0,
  "size": 0,
  "timeout": "60s",
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "someId": [
              "1111"
            ],
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "profile": true,
  "sort": [
    {
      "_id": {
        "order": "asc"
      }
    }
  ],
  "aggregations": {
    "byAttributes": {
      "terms": {
        "field": "groupId.keyword",
        "size": 30,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          },
          {
            "productIds": "asc"
          }
        ]
      },
      "productIds": {
          "scripted_metric": {
            "init_script": {
              "id": "listValuesInit"
            },
            "map_script": {
              "id": "listValuesMap"
            },
            "combine_script": {
              "id": "listValuesCombine"
            },
            "reduce_script": {
              "id": "listValuesReduce"
            },
            "params": {
              "fieldname": "productId"
            }
          }
        },
}

```

ProductId is a string field and the script concatenates unique productids as comma separated value. Now can i sort the aggregations based on `productIds` instead of `_key,_count` or in addition to them.  
If not, what are the work arounds.  
I see this [https://github.com/elastic/elasticsearch/issues/8486](https://github.com/elastic/elasticsearch/issues/8486) is closed, but there is no work around other than doing the sorting on client side.  
Can someone please help me?

---

<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:** [August 5, 2020, 2:04am UTC](https://discuss.elastic.co/t/sort-aggregation-buckets-based-on-keyword-field-scripted-non-metric-field/240246/2 "2020-08-05T02:04:32Z")

</div>

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