Implement the following plan: Enable BM25 Full-Text Search + Hybrid Search in…
- User
Implement the following plan:
Enable BM25 Full-Text Search + Hybrid Search in Turbopuffer
Context
Checkpoint search is currently semantic-only (OpenAI embeddings → Turbopuffer cosine similarity). There's a TODO in
turbopuffer.ts:4to add BM25 full-text search. BM25 excels at exact keyword matches (file names, function names, error messages) where semantic search can miss. Adding both and combining via rank fusion gives the best results.Turbopuffer supports BM25 natively via a
schemadeclaration on upsert (full_text_search: trueon string attributes) and arank_byquery parameter. Thetextattribute already contains the searchable content — we just need to declare it as full-text-searchable and update queries.Files to Modify
File Change api/src/lib/turbopuffer.tsAdd schema to upserts, replace single vector query with hybrid (vector + BM25 via reciprocal rank fusion) api/scripts/test-search-index.tsAdd schema to upsert, add BM25 + hybrid verification steps api/scripts/backfill-search.tsAdd schema to SDK upsert call so backfilled data is BM25-indexed No route, frontend, or env changes needed —
searchCheckpoints()signature stays the same, hybrid is transparent to callers.Implementation
1.
turbopuffer.ts— Add schema toindexCheckpoints()upsertAdd a
schemafield to the upsert request body so thetextattribute gets BM25-indexed:typescriptbody: JSON.stringify({ upserts, distance_metric: "cosine_distance", schema: { text: { type: "string", full_text_search: { language: "english", stemming: true, remove_stopwords: true, }, }, }, }),2.
turbopuffer.ts— ReplacesearchCheckpoints()with hybrid searchCurrent: single vector cosine query on v1 API.
New: fire vector + BM25 in parallel, merge via reciprocal rank fusion.
code Assistant
Now I have all three files. Let me implement the changes.
Assistant
Task 1: Add BM25 schema to
indexCheckpoints()upsertAssistant
Task 2: Replace
searchCheckpoints()with hybrid searchAssistant
Task 3: Add BM25 schema to
backfill-search.tsAssistant
Task 4: Update
test-search-index.tswith schema + BM25/hybrid verificationAssistant
Now update the step counter from
[1/5]through[5/5]to[1/8]through[8/8]since we're adding BM25 and hybrid verification steps.Assistant
Now add BM25 and hybrid verification steps after the existing vector search verification. Let me read the current end of the file to get the exact context.
Assistant
Now replace the existing verification section with vector + BM25 + hybrid verification:
Assistant
Task 5: Type-check
Assistant
Type-check passes with no errors.
Assistant
All done. Let me verify the final state of the main file.
Assistant
Everything looks clean. Here's a summary of all changes:
Changes Made
api/src/lib/turbopuffer.ts- Schema on upsert: Added
full_text_searchschema for thetextattribute (English, stemming, stopword removal) to theindexCheckpoints()upsert body - Hybrid search: Replaced the single vector query in
searchCheckpoints()with parallel vector + BM25 queries merged via Reciprocal Rank Fusion (k=60)- Vector query: v1 API (
/v1/vectors/:ns/query) — same as before - BM25 query: v2 API (
/v2/namespaces/:ns/query) withrank_by: ["text", "BM25", query] - RRF merge:
score(doc) = 1/(60 + rank_vector) + 1/(60 + rank_bm25)— docs in both sets get boosted
- Vector query: v1 API (
- Function signature is unchanged — callers see no difference
api/scripts/backfill-search.ts- Added the same
full_text_searchschema to the SDKns.upsert()call so backfilled data is BM25-indexed
- Schema on upsert: Added
- User
do we need to re-run the backfill or is it just going to work as-is?
Assistant
You need to re-run the backfill. The
schemadeclaration on upsert tells Turbopuffer to build a BM25 index for thetextattribute. Existing data that was upserted without the schema won't have BM25 tokens indexed — so BM25 queries will return nothing (or miss those documents).Two options:
-
Re-run the full backfill —
npx tsx scripts/backfill-search.tswill re-upsert all vectors with the schema, which triggers BM25 indexing on the existingtextattribute. Since the IDs are deterministic (FNV hash of checkpoint_id), it's an idempotent overwrite. -
Schema-only update (cheaper) — You could do a single upsert with 0 vectors but the schema declaration to the namespace, which might trigger a retroactive reindex. But Turbopuffer's docs don't guarantee that — safer to just re-run the backfill.
I'd go with option 1. The embedding cost is the main expense, but since the vectors are the same, you could also consider adding a
--skip-embeddingmode that re-upserts existing vectors with just the new schema. Want me to add that?-
- User
okay, we can do the re-index. in that case, while we're at it. let's set up the necessary plumbing to use the right namespace instead of the hardcoded one right now.
Assistant
The fix is straightforward —
repoIdis already passed tosearchCheckpoints()but ignored. Let me write the plan.- User
[Request interrupted by user for tool use]