How to Paginate Whoosh Search Results with Offsets and Page Counts
Whoosh, the pure-Python full-text search library, includes a built-in helper called search_page that simplifies paginating search results in web applications. The method returns a ResultsPage object carrying metadata such as total hit count, current page number, total page count, and a flag to determine when to hide the 'Next' button. Unlike manual slicing with results[20:30], search_page avoids common pitfalls like off-by-one errors and inaccurate totals, and it only computes enough of the result set to serve the requested page. If a user requests a page number beyond the available range, Whoosh automatically clamps it to the last valid page rather than returning an error or empty result. Developers are advised to render results inside the searcher context block, as hit data is tied to the active searcher instance.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in