Sorting by date and score fails

My mapping:

{
    "id": {
        "type": "long"
    },
    "type": {
        "type": "keyword",
        "ignore_above": 32766
    },
    "text": {
        "type": "text"
    },
    "image":{
        "type": "text"
    },
    "slug": {
        "type": "text"
    },
    "date": {
        "type": "date",
        "format": "strict_date"
    }
}

My query:

{
  "sort":[
     "_score",
     {"date":{"order":"asc"}}
  ],
  "query": {
    "match_phrase" : { "text" : "terminal 5" }
  }
}

My results: (Check lines tagged with WRONG)

    "hits" : [
      {
        "_index" : "global_search",
        "_type" : "_doc",
        "_id" : "10295",
        "_score" : 19.130016,
        "_source" : {
          "id" : 10295,
          "type" : "venue",
          "text" : "Terminal 5", #This is fine, I expect it to appear first
          "weight" : 1,
          "image" : null,
          "slug" : "terminal-5", 
          "date" : null 
        },
        "sort" : [
          19.130016,
          9223372036854775807
        ]
      },
      {
        "_index" : "global_search",
        "_type" : "_doc",
        "_id" : "102718",
        "_score" : 14.129013,
        "_source" : {
          "id" : 705718,
          "type" : "event",
          "text" : "Kcon at Terminal 5, New York",
          "weight" : 2,
          "image" : null,
          "slug" : "09-01-2022-kcon-new-york-ny/",
          "date" : "2022-09-01" #Seems ok
        },
        "sort" : [
          14.129013,
          1661990400000
        ]
      },
      {
        "_index" : "global_search",
        "_type" : "_doc",
        "_id" : "693747",
        "_score" : 14.129013,
        "_source" : {
          "id" : 693747,
          "type" : "event",
          "text" : "Dayglow at Terminal 5, New York",
          "weight" : 2,
          "image" : null,
          "slug" : "11-17-2022-dayglow-new-york-ny",
          "date" : "2022-11-17" #WRONG should appear after the next record
        },
        "sort" : [
          14.129013,
          1668643200000
        ]
      },
      {
        "_index" : "global_search",
        "_type" : "_doc",
        "_id" : "185824",
        "_score" : 13.957819,
        "_source" : {
          "id" : 685844,
          "type" : "event",
          "text" : "311 at Terminal 5, New York",
          "weight" : 2,
          "image" : null,
          "slug" : "10-01-2022-311-new-york-ny/",
          "date" : "2022-10-01" # WRONG I would expect this result to come up first
        },
        "sort" : [
          13.957819,
          1664582400000
        ]
      }

Hi @pthreat

I don't see anything wrong. You have a multilevel sorting and the first level is the score.
By _score the ordering is correct. It turns out that the ids 705718 and 693747 have the same score and the tie was broken by the date and so 705718 was ahead of 693747.

As for the id 185824, it has a smaller _score and that's why it's in the last position.

Thanks for your reply, 2022-10-01 has to come before 2022-11-17

Ok but you sort by _score.

{
  "_id" : "693747",
  "_score" : 14.129013,
  "date" : "2022-11-17"
}
{
  "_id" : "685844",
  "_score" : 13.957819,
  "date" : "2022-10-01"
}

By ordering the id 693747 will be in front of 685844

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