Skip to content

feat(search): add SearchIndex.toJSON() / fromJSON() for pre-built static indices - #11

Open
ashishbhateja wants to merge 1 commit into
mainfrom
feat/search-serialise
Open

ashishbhateja wants to merge 1 commit into
mainfrom
feat/search-serialise

Conversation

@ashishbhateja

Copy link
Copy Markdown
Owner

What

Adds two methods to SearchIndex in src/search.js:

  • index.toJSON() — serialises the built index to a plain, JSON-safe object { postings, docs }. Throws if called before build(), consistent with search().
  • SearchIndex.fromJSON(snapshot) — static factory that reconstructs a fully operational SearchIndex from that snapshot. The docId keys (strings in JSON) are coerced back to numbers to match the internal representation.

Three new smoke checks in scripts/smoke.mjs verify:

  1. toJSON() throws before build()
  2. A fromJSON round-trip returns identical results to the original index for the same query
  3. The snapshot survives JSON.stringify → JSON.parse and is still searchable

All 22 checks pass; npm run build still succeeds.

Why — advances issue #3

Issue #3 asks for archive-wide search across editions. The prerequisite for that is a serialisable index: the build pipeline can call index.build(articles).toJSON() per edition, write the snapshot to dist/ as a static asset, and the client loads whichever edition snapshots it needs — no re-building at query time. Multiple snapshots can also be merged by unioning their docs arrays and re-keying the postings, keeping everything on-device and dependency-free.

This PR adds only the engine capability; wiring it into build.mjs and the reader UI is a natural follow-on.

Scope

Engine-only (src/search.js + scripts/smoke.mjs). No UI changes, no new dependencies, no breaking changes to the existing build() / search() / snippet() / tokenize() API.


Generated by Claude Code

Enables the index to be serialised to a plain JSON-safe snapshot and
reconstructed from it. The build pipeline can now write a pre-built index
per edition to disk and serve it as a static asset, which is the
foundation for archive-wide search without a client-side rebuild step
(issue #3).

Three new smoke checks: toJSON throws before build, fromJSON round-trip
produces identical results, snapshot survives JSON.stringify/parse.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant