fix(core): implement pagination for search() and searchCollection() (#1463)#1578
Open
marcusbellamyshaw-cell wants to merge 1 commit into
Open
Conversation
…mdash-cms#1463) The search API advertised keyset pagination through its types (SearchOptions.cursor, SearchResponse.nextCursor) but searchWithDb never read the incoming cursor nor populated nextCursor, so results were silently capped at `limit` with no way to fetch a second page — "load more" buttons wired against the documented shape never appeared. Results are merged from per-collection FTS queries and re-sorted by score, so there is no single stable keyset column to encode the way getEmDashCollection does. Page the merged, score-sorted set by offset instead, carried opaquely in the cursor's orderValue (reusing encodeCursor/decodeCursor for the same base64-JSON shape and InvalidCursorError handling). Each collection fetches its top (offset + limit + 1) rows so the merged window ranks correctly and a further page is detectable; a nextCursor is issued only when more results exist past the page. searchCollection() gets the same treatment. The /_emdash/api/search endpoint now accepts a `cursor` query param and returns nextCursor; a malformed cursor surfaces as a 400 INVALID_CURSOR via the existing handleError mapping. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: fb940ac The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
Could not push formatting changes to this fork. The contributor may have "Allow edits by maintainers" disabled. Please run the formatter locally: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The search API advertised keyset pagination through its types —
SearchOptions.cursorandSearchResponse.nextCursor— butsearchWithDbnever read the incoming cursor nor populatednextCursor. Results were silently capped atlimitwith no way to fetch a second page, so any "load more" wired against the documented shape got a button that never appears (and acursorthat's discarded).This implements real pagination for both
search()andsearchCollection().Why offset, not pure keyset: search results are merged from per-collection FTS queries and re-sorted by
bm25score, so there's no single stable keyset column to encode the waygetEmDashCollectionencodes(orderValue, id). Instead we page the merged, score-sorted set by offset, carried opaquely in the cursor'sorderValue— reusingencodeCursor/decodeCursorso the cursor keeps the same base64-JSON shape andInvalidCursorErrorhandling as the rest of the API.Each collection fetches its top
(offset + limit + 1)rows, which is exactly what's needed for the merged window[offset, offset + limit)to rank correctly even when the whole window comes from one collection, plus one row to detect a further page. AnextCursoris issued only when at least one result exists past the page.search()/searchWithDbandsearchCollection()both honourcursorand returnnextCursor./_emdash/api/searchendpoint now accepts acursorquery param and returnsnextCursor; a malformed cursor surfaces as400 INVALID_CURSORvia the existinghandleErrormapping.Closes #1463
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
New
tests/integration/search/pagination.test.tscovers: nextCursor presence past the limit, omission on the final page, a cursor walk over disjoint pages covering every match exactly once, malformed-cursor rejection, and single-collection pagination.