Keyword search and BM25
What it is
Keyword search (also called sparse or lexical search) finds chunks that contain the words of the query. BM25 is the standard scoring formula. It gives a chunk a higher score when:
- a query word appears in it more often (with diminishing returns);
- that word is rare across the collection (a rare word such as
KK-4471tells you far more than a common one such asorder); - the chunk is short (a match in a short passage means more than the same match in a long one).
Why it matters
It has been the backbone of search engines for decades, and it is still strong: exact product names, error codes, people's names and legal terms are matched precisely, with no model and no training. Every interviewer expects you to know what it does and why it has not been replaced.
In the real world: A parts distributor's customers search by exact part number; keyword search finds the single matching page instantly, where semantic search returns a dozen similar-looking parts.
Visual example
Computed BM25Keyword scores for “refund for late order”. Rare words and full matches push a passage up.
Filler words such as 'for', 'the' and 'your' are ignored before scoring.
Query: “refund for late order”. BM25 score of each help-centre passage:
- c1 Late order refund: 3.08
- c2 Damaged item refund: 1.02
- c3 Delivery fee: 0.00
- c4 Kart Plus membership: 0.62
- c5 Change address: 0.76
- c6 Error KK-4471: 0.00
c1 Late order refund wins because it contains all three query words. Passages sharing only one common word score low, and those sharing none score zero.
Assuming keyword search is obsolete because embeddings exist. On questions that contain an exact identifier, BM25 regularly beats a good embedding model. Leaving it out is the most common avoidable weakness in a RAG design.
Practise it: Design a Semantic Search System