# Sort items that have equal score

**URL:** <https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022>\
**Category:** Elasticsearch\
**Created:** [December 15, 2017, 5:09pm UTC](https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022 "2017-12-15T17:09:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iitgrad](https://avatars.discourse-cdn.com/v4/letter/i/9de053/32.png) [@iitgrad](https://discuss.elastic.co/u/iitgrad)\
**Post date:** [December 15, 2017, 5:09pm UTC](https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022/1 "2017-12-15T17:09:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 22, 2017, 12:57pm UTC](https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022/2 "2017-12-22T12:57:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iitgrad](https://avatars.discourse-cdn.com/v4/letter/i/9de053/32.png) [@iitgrad](https://discuss.elastic.co/u/iitgrad)\
**Post date:** [December 22, 2017, 2:07pm UTC](https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022/3 "2017-12-22T14:07:58Z")

</div>

Ah awesome thanks Luis

---

<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:** [January 19, 2018, 2:08pm UTC](https://discuss.elastic.co/t/sort-items-that-have-equal-score/112022/4 "2018-01-19T14:08:08Z")

</div>

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