How to populate the top 5 series from the time-series data( which has over 100 series)

Hello All,

We have the grafana dashboard using the loki datasource , which populates the series of nginx api end points in time series . We have around 100 series over the time range. We would like to populate only top 5 api endpoints over the time which has been used more. We have tried the topk function over the query but no luck. Could anyone help us on this.

Here are the stack information:
Loki: v2.8.2
Grafana: v9.4.3

Loki query: sum(count_over_time({job=“default/hproxy”} |~“GET|POST|OPTIONS” |~ "/api|/ " | pattern <_> - - <_> "<method> <msg> <_>" <_> <_> <_> "<_>" <_> [$__interval])) by (msg)