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

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 is closed, but there is no work around other than doing the sorting on client side.
Can someone please help me?

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