# Get distinct term values but order by another value

**URL:** <https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012>\
**Category:** Elasticsearch\
**Created:** [October 31, 2019, 9:52am UTC](https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012 "2019-10-31T09:52:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Weeda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/j_weeda/32/45092_2.png) [@J\_Weeda](https://discuss.elastic.co/u/J_Weeda)\
**Post date:** [October 31, 2019, 9:52am UTC](https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012/1 "2019-10-31T09:52:25Z")

</div>

Hello, I struggling with a query to get the unique values for a term called `metric` from my index while sorting the result on a different (numeric) field: `metric_order`.

Consider below example:

```
metric: metric_order:
A 0
B 3
C 1
D 2

```

My requirement is to get A, C, D, B as per metric\_order

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 31, 2019, 12:53pm UTC](https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012/2 "2019-10-31T12:53:23Z")

</div>

The `terms` aggregation has a few paragraphs about ordering on other fields, that might be what you are after. See [https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order)

---

<div class="post-metadata">

**Author:** ![J\_Weeda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/j_weeda/32/45092_2.png) [@J\_Weeda](https://discuss.elastic.co/u/J_Weeda)\
**Post date:** [November 8, 2019, 12:05pm UTC](https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012/3 "2019-11-08T12:05:06Z")

</div>

ty. I had already gone through that page but revisiting it seems that below setup may just do the trick

```
GET /_search
{
    "aggs" : {
        "genres" : {
            "terms" : {
                "field" : "genre",
                "order" : { "max_play_count" : "desc" }
            },
            "aggs" : {
                "max_play_count" : { "max" : { "field" : "play_count" } }
            }
        }
    }
}

```

I'll give it a go.

---

<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:** [December 6, 2019, 12:05pm UTC](https://discuss.elastic.co/t/get-distinct-term-values-but-order-by-another-value/206012/4 "2019-12-06T12:05:13Z")

</div>

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