What is BM25?
BM25 (Best Matching 25) is a ranking function widely used in information retrieval to score the relevance of documents to a search query based on term frequency and document length. It improves upon earlier approaches like TF-IDF by incorporating probabilistic relevance models and including saturation functions that prevent term frequency from dominating the score. BM25 has become a standard baseline for keyword-based search systems.
The algorithm calculates relevance scores by considering how often query terms appear in a document (term frequency), how rare those terms are across the entire document collection (inverse document frequency), and the length of the document relative to the average document length. The saturation function ensures that repeatedly mentioning a term has diminishing returns, which helps prevent spam-like content from ranking too highly. Tunable parameters allow customization of term frequency saturation and document length normalization.
In the context of modern AI systems, BM25 is often used as the sparse retrieval component in hybrid search systems, complementing dense retrieval methods based on embeddings. This combination leverages BM25's strength at exact keyword matching and its ability to handle rare or domain-specific terms, while dense retrieval handles semantic similarity. BM25 remains relevant in RAG systems and agent memory architectures as a reliable, interpretable, and computationally efficient retrieval method.