New Tool Radar

AI Concepts

How to Combine BM25 Keyword Search With Vector Search for RAG

Run dense vector search and BM25 keyword search together so exact terms and meanings both match.

Schematic with three steps: Run the dense vector search; Run the BM25 keyword search; Combine the two ranked lists.

Dense embeddings are good at broad meaning, but they often miss an exact keyword: a serial number, an error code, or a specialised acronym. Combining dense vector search with a sparse keyword search such as BM25 gives you both semantic matches and exact-term matches. In PostgreSQL, full-text operators can run alongside pgvector cosine similarity, and the two scores are normalised and combined.

What you need

  • A vector store with your chunks embedded
  • A keyword index over the same chunks, such as BM25 or PostgreSQL full text
  • A test question containing an exact term, such as an error code

Step 1: Run the dense vector search

Embed the question and retrieve the top results by vector similarity. Note which results match the meaning but miss the exact term.

Step 2: Run the BM25 keyword search

Run the same question as a keyword search and note which results match the exact term, such as the error code or serial number, even when the wording differs.

Step 3: Combine the two ranked lists

Normalise the two scores and combine them, for example with reciprocal rank fusion, so a chunk that scores well on either list can reach the shortlist. Check the combined top results contain both the exact term and the meaning.

Worked example: synthetic hybrid search on an error code

The table below is a synthetic example. It shows the expected result of a hybrid search; no search was run.

Search Top result Has the error code?
Vector only General troubleshooting page No
BM25 only Page with error code E-1042 Yes
Hybrid combined Page with error code E-1042 Yes
Check Exact term found and on-topic Pass

Expected result (UNTESTED). vector search alone misses the error code, BM25 alone finds it, and the hybrid list keeps it at the top. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Hybrid search is not always better: in one published 156-query benchmark, dense-only beat hybrid, so measure on your own corpus.
  • Pure vector search misses exact matches and pure keyword search misses semantics; the combination covers both failure modes.
  • Normalising two different score scales is a real step; a naive sum can let one list dominate.
  • Reciprocal rank fusion is one combining method; check the fused ranking on your test questions before shipping it.

Sources

See all