What is AI BM25?
BM25 is a probabilistic keyword scoring function that ranks documents by how often query terms appear in them, balanced against document length and how rare each term is in the collection.
It differs from Term Frequency-Inverse Document Frequency in two corrections: TF-IDF simply multiplies term frequency by inverse document frequency, while BM25 adds saturation for term frequency and document length normalisation. Repeating a term does not raise the score without limit; long documents do not win by default.
- Topic
- Embeddings, document processing and RAG
- Also known as
- Okapi BM25, Best Matching 25
- Origin
- 1994, Okapi system
How AI BM25 works
- For each document, query term frequency, document length and the number of documents containing each term are computed.
- Inverse document frequency gives rare, distinctive terms more weight than common words.
- Term frequency's contribution is capped by a saturation curve and the score is normalised by document length; two tuning parameters set this behaviour.
- Scores for all query terms are summed and documents are ranked by the total.
In more detail
The role of the k1 and b parameters
BM25 is controlled by two main hyperparameters: k1 (typically 1.2 to 2.0), which sets how quickly repeated term occurrences saturate and b (typically 0.75), which controls the strength of document length normalisation against the collection average. Tuning k1 and b allows teams to adapt retrieval for short structured fields like titles versus long technical manuals.
Hybrid fusion (RRF) and multi-field BM25F
In hybrid search, BM25 rankings and dense vector rankings operate on incompatible score scales. Methods such as Reciprocal Rank Fusion (RRF) (which combines document ranks rather than raw scores) or min-max score normalisation merge both lists into a single reliable ranking. For multi-field documents (such as title, abstract and body), the BM25F variant weights each field separately before scoring.
Word order blindness and the synonym limit
Because BM25 follows a bag-of-words assumption, it ignores word order, phrase structure and synonyms unless paired with query expansion or semantic retrieval. Two words appearing far apart in a long document receive the same base BM25 match score as an exact adjacent phrase, so proximity boosts or phrase queries are often layered on top.
Production implementations: Lucene, PostgreSQL and BM25S
Modern production stacks run BM25 inside inverted-index engines such as Apache Lucene (Elasticsearch/OpenSearch), PostgreSQL extensions (pg_search/ParadeDB), vector databases with native sparse support or ultra-fast pure-Python/Cython libraries like BM25S for lightweight in-memory RAG pipelines.
Commonly confused terms
| Term | How it differs |
|---|---|
| SPLADE (learned sparse retrieval) | BM25 uses exact lexical token matching with statistical weights and requires no neural model at query time, whereas SPLADE uses a transformer encoder to expand documents and queries into learned sparse vocabulary weights, capturing synonyms while remaining indexable in inverted indices. |
| Dense vector retrieval | BM25 computes exact lexical scores over sparse inverted indices, whereas dense vector retrieval maps text into continuous embeddings to match semantic similarity even when exact keywords differ. |
Why AI BM25 matters for on-premises AI
It runs fast on CPUs without GPUs, so in-house servers can search large collections with no extra hardware. It gives strong, explainable ranking for exact matches such as product codes, error codes and regulation clauses. It is the sparse component of hybrid search; combined with vector search it can improve retrieval accuracy. Turkish suffixes make matching harder, so it is paired with stemming or morphological analysis.
In a manufacturer's maintenance instruction archive a technician enters an error code; BM25 finds exactly matching instructions while vector search adds records with similar descriptions.
Common questions
Is BM25 still needed when vector search exists?
Yes. Vector search finds by meaning but can miss codes, numbers and proper names. BM25 catches those exact matches; the two are combined in hybrid search.
Does BM25 work well on Turkish documents?
Used bare, it can miss matches because of suffixes. With stemming, lemmatisation or morphological analysis its performance improves markedly.
Related terms
- Term Frequency-Inverse Document Frequency
- Sparse retrieval
- Keyword search
- Hybrid search
- Information retrieval
- Reciprocal Rank Fusion
- Reranking
- Morphological analysis
Sources
- The Probabilistic Relevance Framework: BM25 and Beyond, 2009
- Reciprocal rank fusion outperforms Condorcet and individual rank learning methods, SIGIR 2009
Related page on this site: Related FAQ · In the glossary: Glossary, A to D