Is there is a way to sort terms aggregation by string field?

I would like to know if we have possibility to use string field to order buckets of aggregated results.

At the beginning we had an index without duplicates and had a query like that to get all required results:
es_query.json

Now we have some duplicates in this index and would like to aggregate it in some way and keep the order of results. If we would like to sort results for numeric field then it's easy. We can use query like this: es_query_aggr.json

But if we have any string field which we need to use for ordering then we get a error

"type":"aggregation_execution_exception","reason":"Unsupported script value [ben]"

We are getting this error because we can't use max or min (I guess any of metrics aggregations) for non numeric fields.

Is it possible to get the same result as we have for es_query.json query for the query from es_query_aggr.json in any way?

Thanks for any help.

What you are trying to do is not supported. Would it be possible for you to perform deduplication at indexing time? For instance by using this group_by_field field as an id? Note that even if elasticsearch gets query-time deduplication capabilities, an index-time approach would still be significantly more efficient.