Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search sort documents with same score using another field value

I'm using elastic search and I would like to sort documents having same score to the query shown below, on the basis of higher number of - "likes" field - integer type stored in all documents. Code -

query: {
                multi_match: {
                        query: "some cooler",
                        type: "most_fields",
                        fields: ["info1", "info2", "info3"]
                }
        }
like image 589
vjjj Avatar asked Jan 06 '23 16:01

vjjj


1 Answers

You should check the sort documentation

Just add a sort section on json sorting by score followed by your custom field.

{
    "query" : {...},
    "sort" : [
        "_score",
        { "likes" : {"order" : "desc"}}
    ]
}

score order is 'desc' by default. Other fields are 'asc' by default so you need to define 'desc' order for your 'likes' field if that's what you want.

like image 199
Andre85 Avatar answered Apr 18 '23 05:04

Andre85