RAGcore
BM25 (keyword search)
The classic ranking function: rare terms that appear often in a short document score high.
What it is and why it exists
BM25 scores a document by summing, for each query term, its inverse document frequency (rare terms matter more) times a saturating term-frequency factor normalised by document length (k1 controls saturation, b controls length normalisation). It nails exact identifiers, names and jargon that embeddings blur.
Quick prototype
rank_bm25 in Python or Postgres full-text search.
In production, at scale
An inverted index (Elasticsearch/OpenSearch, Postgres FTS, Tantivy) with analysers for your language, field boosts (titles), and BM25 as one half of hybrid retrieval.
Vendors and tools
| Elasticsearch / OpenSearch | BM25 by default |
|---|---|
| Postgres full-text search / ParadeDB | Keyword search in Postgres |
| Tantivy / Lucene | Search engine libraries |
On AWS: Amazon OpenSearch Service; Kendra for managed enterprise search.
How it fails
- Misses paraphrases (“money back” vs “refund”)
- Bad tokenisation of codes like ERR-4012
- No stemming for the corpus language
What the interviewer asks next
Write the BM25 term score.
idf(t) × tf × (k1 + 1) / (tf + k1 × (1 − b + b × |d| / avgdl)), with idf(t) = ln(1 + (N − n + 0.5)/(n + 0.5)); typical k1 ≈ 1.2–2, b ≈ 0.75.
Why keep BM25 when you have embeddings?
Exact identifiers, rare names and new jargon. Users search for error codes and SKUs, which dense retrieval handles poorly.