Dense vectors miss exact terms; keyword search misses meaning. Enterprise documents need both: a query like “section 4.2 termination” wants exact matches, while “how do we end the contract early” wants semantic search. [Your own example goes here.]
The pipeline#
- Parse PDFs to Markdown with pymupdf4llm and split them into chunks.
- Store each chunk with a
tsvectorcolumn and a bge-m3 embedding in pgvector. - Retrieve with BM25, full-text search and vectors, then fuse the rankings.
- Rerank the top candidates with bge-reranker-v2-m3 and send the best to the LLM.
Fusing the rankings#
Scores from different retrievers aren't comparable, so fuse by rank. An example with reciprocal rank fusion:
WITH vec AS (
SELECT id, row_number() OVER (ORDER BY embedding <=> :q_vec) AS r
FROM chunks ORDER BY embedding <=> :q_vec LIMIT 50
), fts AS (
SELECT id, row_number() OVER (ORDER BY ts_rank_cd(tsv, q) DESC) AS r
FROM chunks, plainto_tsquery('simple', :q_text) q
WHERE tsv @@ q LIMIT 50
)
SELECT id, sum(1.0 / (60 + r)) AS score
FROM (SELECT * FROM vec UNION ALL SELECT * FROM fts) hits
GROUP BY id ORDER BY score DESC LIMIT 20;What changed#
[Your measured results: retrieval quality before and after hybrid search and reranking, and the latency the reranker adds.]