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?
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:
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 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) and use that field as a tiebreaker instead.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.