Search code examples
elasticsearchsearchpaginationelastic-stackelasticsearch-dsl

How to calculate the current page while using Search After?


Currently, I am working on Elastic Search and doing pagination the data. Particularly, the data is sorted with 2 fields:

  • date, sort desc
  • id, sort desc when date is same

With the given page size, I can calculate the number of pages by using track_total_hit. However, the problem is that I cannot calculate the current page because the sort key(date, id) do not contain any information about the index of the records.

I have also considered using the from and size, but the number of my data is higher than 10,000 records. So that is impossible to use them. Also, I don't want to change the default configuration of max_result_window because that will affect the performance.

Do you have any solution to solve this? Thank you!

I have checked:


Solution

  • I have researched and figured-out that this is a trade-off.

    That is similar to offset-pagination and keyset-pagination of SQL db.

    The offset strategy is similar to the Scroll API which need to scan from the beginning, and it knows the current page.

    Meanwhile, the search after is similar to the keyset strategy, which do not need to scan from the beginning, but won't know the current page.