A result cursor identifies where to continue a specific ordered search. It is not merely a page number and should not be accepted after the user changes the query, filters, or sort. For a score-based order, the cursor needs the last score and a stable unique tie-breaker. It should also bind the normalized query and filter set, directly or through a server-side reference, so a cursor from one search cannot silently continue another. A bounded result window avoids returning an entire index hit set to the browser. Decide what happens when the index changes between pages: a live feed may allow shifts, while an audit task may need a snapshot.
Search Filters and Result Cursors
Working case
Reviewer 29 filters open cases in region North, then changes the region to South while a request for the next North page is still running. If the old cursor is reused, the list skips or repeats cases in the new region. The UI must clear pagination state when any search dimension changes and ignore a late response from the old search. The server independently validates the cursor against the active query and filter values. A stable case ID breaks score ties so two records with equal relevance do not trade positions.
Implementation boundary
function cursorMatchesSearch(cursor, normalizedQuery, region, status) {
return cursor.query === normalizedQuery && cursor.region === region &&
cursor.status === status && Number.isSafeInteger(cursor.lastCaseId);
}
console.log(cursorMatchesSearch({ query: "lift", region: "north", status: "open", lastCaseId: 47 }, "lift", "south", "open"));
// Output: falseThe illustration checks binding but is not a complete cursor format. Production cursors should be opaque to clients and authenticated or stored server-side so callers cannot alter positions or filters. Apply authorization and filter predicates before selecting the page; fetching an unrestricted page and hiding rows afterward can produce short pages and leak aggregate information. Return the next cursor only when more authorized results exist. Keep the sorting tuple and cursor decoding under version control so a changed ranking formula does not misread older cursors.
Cost and boundaries
Seek pagination can avoid the skip work of large offsets, but a compound score and ID sort needs index or search-engine support. Each page returns O(k) records for page size k; authorization and ranking may inspect more candidates. A snapshot improves consistency but costs state or storage and can retain outdated results. Expiring a cursor is simpler, though users may need to restart a search. Cap page size and cursor age, and measure how many candidates are evaluated to deliver one authorized page.
Failure trace
The UI preserves an old cursor after a filter change, and the backend accepts it because it only decodes lastCaseId. A case disappears from the result set and the user thinks the region has no more work. Another service filters unauthorized hits after limiting, returning three items on a twenty-item page even when more allowed results exist. Bind cursor to search dimensions and apply permission predicates before page selection. Test filter changes, tied scores, expiry, and records inserted between pages.
Verification
- Change query, region, status, and sort independently; old cursors must not continue the new search.
- Page across equal-score records without duplicates or gaps.
- Revoke an in-range record and verify page selection still follows permission rules.
Practice drill
Search for lift, filter North/open, advance one page, then switch to South/open before the earlier request completes. Verify that the old response cannot replace the new page. Alter each cursor dimension and expect rejection or restart. Seed equal scores around a page boundary and check for duplicates or omissions. Revoke access to one record in the middle and measure whether the next page still fills with authorized results when enough remain.
Decision note
Use a cursor when ordered result sets can grow large, and bind it to the complete query context with a defined change policy.
Common Mistakes
- Reusing a cursor after filters change.
- Encoding only an offset or case ID for a relevance-ordered result set.
- Hiding unauthorized records after limiting the page.
Connected lessons
Application Search and Retrieval; Search Index Freshness and Rebuilds; Search Query Normalization and Ranking; Search Permissions and Private Results; Cursor Pagination for Changing Collections; React Effects and Request Races; Authorization: check permission for this record on every request.
Apply and check
Build Project: private case search and review Web Development: search and quality contracts quiz.
