Skip to content
Level up your prompts with SurePrompts — curated prompts for every workflow.
agentscamp

BM25

BM25 is the classic keyword ranking function: it scores a document by how often the query terms appear, how rare they are, and how long the document is.

Updated

BM25 is a keyword ranking function: for each query term it multiplies an IDF weight by a saturating, length-normalized term-frequency factor, then sums across terms. k1 controls saturation and b controls length normalization. Lucene and Elasticsearch default to k1 = 1.2 and b = 0.75; those are defaults, not optimal values.

BM25 is a ranking function for keyword search: it scores a document against a query by summing, over the query terms, each term's rarity in the collection multiplied by how often it appears in the document, with that frequency capped by a saturation curve and adjusted for document length.

It comes from the probabilistic relevance framework. Robertson and Zaragoza's 2009 review, "The Probabilistic Relevance Framework: BM25 and Beyond," calls it "the best-known instantiation of the model" and credits the original function to Robertson and Walker in 1994. Without relevance information, the classic per-term weight is:

w(t)   = tf / ( k1 · ((1 − b) + b · dl / avdl) + tf ) · IDF(t)
IDF(t) = log( (N − n_t + 0.5) / (n_t + 0.5) )

Here tf is the term's frequency in the document, dl the document length, avdl the average length, N the number of documents and n_t the number containing the term. A common variant multiplies the numerator by (k1 + 1), which does not change the ranking. Lucene computes IDF as log(1 + (docCount − docFreq + 0.5) / (docFreq + 0.5)).

The two parameters are the whole tuning surface. k1 sets saturation, so a term's contribution "cannot exceed a saturation point" however often it repeats. b sets length normalization: 1 normalizes fully, 0 switches it off. Lucene and Elasticsearch default to k1 = 1.2 and b = 0.75. Treat those as defaults, not optimal values: the review says the model "provides no guidance on how these should be set."

Because BM25 matches exact tokens, it catches error codes and identifiers that embeddings blur, which is why hybrid search pairs it with a dense index such as HNSW. Fusion and reranking are covered in Hybrid Search & Reranking.

Frequently asked questions

What do k1 and b do in BM25?
k1 controls term-frequency saturation: repeating a term keeps adding less, and one term's contribution cannot exceed a saturation point however often it occurs. b controls document-length normalization, where 1 is full normalization and 0 switches it off. Robertson and Zaragoza note that the model itself gives no guidance on setting them, and that values such as 0.5 < b < 0.8 and 1.2 < k1 < 2 are reasonably good in many circumstances.
Are k1 = 1.2 and b = 0.75 the best BM25 values?
They are defaults, not proven optima. Lucene's BM25Similarity and Elasticsearch both default to k1 = 1.2 and b = 0.75. The standard review of BM25 reports evidence that optimal values depend on other factors, while also calling BM25 quite robust with respect to its parameters, so tune against your own relevance judgments if you change them at all.
How does BM25 fit into hybrid search?
BM25 supplies a ranked list of exact-term matches and a vector index supplies a ranked list of semantic matches; a fusion step merges them. Elasticsearch's Reciprocal Rank Fusion adds 1/(k + rank) for each list a document appears in, with a default rank constant of 60, and needs no tuning because the two score scales never have to be compared.

Filed under

bm25 · keyword-search · sparse-retrieval · ranking · hybrid-search

Related