# Using search\_after for pagination where you sort by score

**URL:** <https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233>\
**Category:** Elasticsearch\
**Created:** [August 27, 2018, 7:04pm UTC](https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233 "2018-08-27T19:04:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![i333](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@i333](https://discuss.elastic.co/u/i333)\
**Post date:** [August 27, 2018, 7:04pm UTC](https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233/1 "2018-08-27T19:04:21Z")

</div>

In my query I sort by score, so the most relevant results are first. I'd like to be able to paginate this, and `search_after` is apparently the best way for my use case.

But `search_after` doesn't seem to let you use `_score` as a sort field. I'd need to sort by something like `_id` or whatever which is not good, because then in the initial search the results are ordered by id and not relevance.

What's the correct way to use search\_after for pagination while keeping the results ordered by relevance?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 28, 2018, 11:55am UTC](https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233/2 "2018-08-28T11:55:31Z")

</div>

It's definitely possible to sort on `_score` when using `search_after`. You would typically use `_score` as the primary sort order, and `_id` as the secondary sort order. That way, the results are still ranked on relevance primarily, but if two documents have the same score, they will be sorted on their `_id`.

This is what such a request's sort clause would like like:

```auto
  "sort": [
    {"_score": {"order": "desc"}},
    {"_id": {"order":"asc"}}
  ]

```

---

<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 25, 2018, 11:55am UTC](https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233/3 "2018-09-25T11:55:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 12, 2019, 8:28am UTC](https://discuss.elastic.co/t/using-search-after-for-pagination-where-you-sort-by-score/146233/4 "2019-08-12T08:28:17Z")

</div>

Replying to this old topic, as a community member pointed out to me that the earlier advice that I provided in November 2018 is outdated.

[Sorting on `_id` is not recommended](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-search-after) as doc values are not enabled for that field. As a result, you may run out of memory by using fielddata. What you want to do instead is copy the value of `_id` to another field (for example using an [ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html)) and use that field as a tiebreaker instead.
