Search code examples
sortingelasticsearch

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"]
                }
        }

Solution

  • 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.