Sort items that have equal score


(Kevin McQuown) #1

What is the proper way to structure a query so that using a should with several values allows proper scoring and then does a sort on a date field but only if the scores are the same. ie. first sort by score and then subsort by date.


(Luiz Santos) #2

Hi @iitgrad,

You can use two fields in your sort clause, the first should be _score:

POST posts/doc/1
{
  "body": "same body",
  "date": "2017-12-18T00:00:00"
}

POST posts/doc/2
{
  "body": "same body",
  "date": "2017-12-08T00:00:00"
}

GET posts/_search
{
  "query": {
    "match": {
      "body": "same body"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "date": {"order": "asc"}
    }
  ]
} 

Hope it helps.

Cheers,
LG


(Kevin McQuown) #3

Ah awesome thanks Luis


(system) #4

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