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

**URL:** <https://discuss.elastic.co/t/is-there-is-a-way-to-sort-terms-aggregation-by-string-field/37926>\
**Category:** Elasticsearch\
**Created:** [December 24, 2015, 10:12am UTC](https://discuss.elastic.co/t/is-there-is-a-way-to-sort-terms-aggregation-by-string-field/37926 "2015-12-24T10:12:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliskovets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliskovets/32/6806_2.png) [@eliskovets](https://discuss.elastic.co/u/eliskovets)\
**Post date:** [December 24, 2015, 10:12am UTC](https://discuss.elastic.co/t/is-there-is-a-way-to-sort-terms-aggregation-by-string-field/37926/1 "2015-12-24T10:12:39Z")

</div>

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](https://gist.github.com/eliskovets/665b0831556e8192fee7/raw/57e51bcd185403b5acf00d4d2424dc7055cc74db/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](https://gist.githubusercontent.com/eliskovets/665b0831556e8192fee7/raw/57e51bcd185403b5acf00d4d2424dc7055cc74db/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.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 24, 2015, 12:59pm UTC](https://discuss.elastic.co/t/is-there-is-a-way-to-sort-terms-aggregation-by-string-field/37926/2 "2015-12-24T12:59:00Z")

</div>

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.

---

<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:** [July 5, 2017, 11:28pm UTC](https://discuss.elastic.co/t/is-there-is-a-way-to-sort-terms-aggregation-by-string-field/37926/3 "2017-07-05T23:28:50Z")

</div>


