# 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.

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:

```text
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](/glossary/hybrid-search) pairs it with a dense index such as [HNSW](/glossary/hnsw). Fusion and reranking are covered in [Hybrid Search & Reranking](/guides/concepts/hybrid-search-reranking).

---

_Source: https://agentscamp.com/glossary/bm25 — Term on AgentsCamp._
