Sort items that have equal score

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.

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

Ah awesome thanks Luis

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