Get distinct term values but order by another value

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

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

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.

1 Like

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