# Sorting by date and score fails

**URL:** <https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014>\
**Category:** Elasticsearch\
**Created:** [August 12, 2022, 12:12pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014 "2022-08-12T12:12:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pthreat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pthreat/32/64966_2.png) [@pthreat](https://discuss.elastic.co/u/pthreat)\
**Post date:** [August 12, 2022, 12:12pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014/1 "2022-08-12T12:12:47Z")

</div>

My mapping:

```auto
{
    "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:

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

```

My results: (Check lines tagged with WRONG)

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

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [August 12, 2022, 12:26pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014/2 "2022-08-12T12:26:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pthreat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pthreat/32/64966_2.png) [@pthreat](https://discuss.elastic.co/u/pthreat)\
**Post date:** [August 12, 2022, 12:28pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014/3 "2022-08-12T12:28:51Z")

</div>

> [@pthreat](#):
>
> `2022-10-01`

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

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [August 12, 2022, 12:33pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014/4 "2022-08-12T12:33:18Z")

</div>

Ok but you sort by \_score.

```auto
{
  "_id" : "693747",
  "_score" : 14.129013,
  "date" : "2022-11-17"
}

```

```auto
{
  "_id" : "685844",
  "_score" : 13.957819,
  "date" : "2022-10-01"
}

```

By ordering the id 693747 will be in front of 685844

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 9, 2022, 12:33pm UTC](https://discuss.elastic.co/t/sorting-by-date-and-score-fails/312014/5 "2022-09-09T12:33:39Z")

</div>

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