Learning note
Hybrid retrieval with PostgreSQL FTS and pgvector
Keep lexical and semantic evidence independently testable, with model-aware visibility and bounded fusion.
Two independently useful branches
Technical names benefit from lexical matching. Related concepts can benefit from vector similarity even when their wording differs. Ask Habib’s next retrieval layer combines those signals while retaining the original page, section and evidence label for every accepted chunk.
The public search endpoint stays lexical. Phase 6 adds private operator commands for semantic backfill and retrieval. A pinned multilingual E5 model runs on the owner’s Mac and produces 384-dimensional vectors. The database commands consume those vectors without an external inference API, provider key, or always-on model service. Cached real-model vectors let ordinary CI run offline.
Publish one coherent vector space
The vector-space fingerprint includes provider, model, dimensions, exact model and tokenizer artifact hashes, and the text preparation version. A same-dimension model change requires an explicit full rebuild. A dimension change requires new storage rather than writing different widths into the current column.
The semantic job shares the lexical indexer’s advisory lock on one dedicated PostgreSQL connection. It verifies the exact published corpus, stages all required vectors outside a write transaction, verifies publication again, then atomically updates vectors and the semantic corpus marker. Provider or SQL failure preserves the previous committed vectors. Unchanged inputs can reuse their validated cache.
A lexical content update can make semantic state stale. Retrieval checks that marker and vector coverage, and keeps the lexical branch available while semantic backfill awaits an operator. The private retrieval query reads both branches and index state in one repeatable-read transaction, filtering the expected content fingerprints and active vector space.
Accept before combining
Raw similarity and lexical signals decide which candidates qualify. RRF then orders accepted candidates. A weak result cannot become relevant merely by being the first item in a weak list. The final evidence set has page, count and combined size caps. Full stored evidence remains plain text with its original label.
Synthetic vectors test contracts and failure behavior; separate cached MiniLM vectors measure retrieval against actual portfolio content. Similarity is topical evidence, not proof that a passage answers a question. Unsupported claims and Indonesian queries need explicit evaluation; an English-focused model cannot be assumed to handle both well. Production activation requires a separate execution approval. The choices are recorded in ADR-019 and ADR-009.