Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch two level sort in aggregation list

Currently I am sorting aggregations by document score, so most relevant items come first in aggregation list like below:

{
            'aggs' : {
                'guilds' : {
                    'terms' : {
                        'field' : 'guilds.title.original',
                        'order' : [{'max_score' : 'desc'}],
                        'aggs' : {
                            'max_score' : {
                                'script' : 'doc.score'
                            }
                        }
                    }
                }
            }
        }

I want to add another sort option to the order terms order array in my JSON. but when I do that like this :

{
        'order' : [{'max_score' : 'desc'}, {"_count" : "desc"},
    }

The second sort does not work. For example when all of the scores are equal it then should sort based on query but it does not work.

like image 490
Reza Shadman Avatar asked Dec 07 '22 00:12

Reza Shadman


1 Answers

As a correction to Andrei's answer ... to order aggregations by multiple criteria, you MUST create an array as shown in Terms Aggregation: Order and you MUST be using ElasticSearch 1.5 or later.

So, for Andrei's answer, the correction is: "order" : [ { "max_score": "desc" }, { "_count": "desc" } ]

As Andrei has it, ES will not complain but it will ONLY use the last item listed in the "order" element.

like image 188
Thomas Doman Avatar answered Dec 21 '22 15:12

Thomas Doman