# BM25

> Canonical: https://skillalmanac.com/glossary/bm25 | Last verified: 2026-11-24 | Index status: not offered to search engines

BM25 (Best Matching 25) is a ranking function that scores documents against a query by lexical matching, meaning it looks for the query's actual words. It builds on TF-IDF and adjusts for document length and repeated terms.

Also called: BM25 algorithm, Okapi BM25, BM25 search, BM25 ranking, BM25 scoring.

## In plain language

BM25 is the scoring rule behind classic keyword search. Given a search, it gives every document a score based on how many of your words it contains. Rare words count for more than common ones, a word that appears again and again stops adding much, and a short page is not unfairly beaten by a long one. Searching a help centre for the error code TS-999 is a good fit, because the exact string matters. The limit is that it matches words, not meaning. A search for car will not find a page that only says automobile. Many AI document-search systems therefore run it alongside a meaning-based search and merge the two result lists.

## Technical definition

For each query term it multiplies an inverse document frequency weight, which rewards terms that are rare across the collection, by a saturating term-frequency component. Elastic's explainer describes the factors as term frequency, inverse document frequency and document length, with parameter k1 limiting how much one term can affect a document's score and parameter b scaling the effect of document length. Elastic gives default values of b = 0.75 and k1 = 1.2. Anthropic's contextual retrieval post describes BM25 as building on TF-IDF and presents it as complementary to embeddings, combining the two with rank fusion. It also prepends chunk-specific context before building the BM25 index.

## How it shows up when you use agents

If you build or use a document-search setup for an agent, BM25 is often the keyword half of it. An [embedding](/glossary/embedding) search finds passages with similar meaning, and BM25 finds passages that contain the exact words, such as an error code, a function name or a product identifier. Anthropic's contextual retrieval post describes using both and merging the results. That combined approach is often called [hybrid search](/glossary/hybrid-search), and it sits inside many [retrieval-augmented generation](/glossary/retrieval-augmented-generation) pipelines, often followed by [reranking](/glossary/reranking).

Coding agents also search by exact text when they look through your repository, which is closer to keyword matching than to meaning-based search. That works well for names and strings and less well for questions phrased in different words from the code.

What breaks when BM25 is misunderstood: people expect it to understand synonyms and are surprised when a paraphrased question finds nothing, or they test only with exact-word queries and miss the failure. The next action is to test your retriever with questions that use different words from the documents, and to look at how [chunking](/glossary/chunking) splits the text, since BM25 scores each chunk separately.

## Common confusions

- **BM25 versus semantic search.** BM25 matches the words in the query. [Semantic search](/glossary/semantic-search) compares meaning using embeddings. Each can find things the other misses.
- **BM25 versus TF-IDF.** BM25 builds on TF-IDF. According to Anthropic's post, it also accounts for document length and saturates term frequency so common words do not dominate.
- **BM25 versus a vector database.** A [vector database](/glossary/vector-database) stores embeddings for similarity search. BM25 is a scoring formula over words and works without one.
- **Myth.** Keyword search is obsolete once you have embeddings. Anthropic's post presents BM25 as complementary, because embeddings can miss exact matches.

## Worked examples in this catalogue

### [What Is RAG? Retrieval-Augmented Generation Explained](https://skillalmanac.com/learn/what-is-rag)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A walkthrough of indexing and querying for RAG, including how chunk boundaries affect what retrieval finds and a technique of adding a short context line to each chunk, which the guide credits to Anthropic's contextual retrieval write-up.

Why it exercises the concept: BM25 is one of the retrieval methods such pipelines use. The guide shows the chunking step that decides what any retriever, keyword or meaning-based, can find.

### [rag-chunking-planner](https://skillalmanac.com/skills/skill-almanac/rag-chunking-planner)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Your documents split by three chunking strategies, with defects and offline recall reported for each, so chunk settings are chosen with numbers.

Why it exercises the concept: BM25 scores the chunks you give it, so chunk size and boundaries change what it can match. This skill measures that step before you pick settings.

### [keyword-density-count](https://skillalmanac.com/skills/skill-almanac/keyword-density-count)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Case-insensitive, whole-word counts of each keyword or phrase in plain page text, with density figures reported.

Why it exercises the concept: It shows raw term counts, the simplest ingredient of lexical scoring. It does not rank documents, weight rare words or adjust for length, which is what BM25 adds.

## Related terms

- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)
- [Semantic search](https://skillalmanac.com/glossary/semantic-search.md)
- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Chunking](https://skillalmanac.com/glossary/chunking.md)
- [Reranking](https://skillalmanac.com/glossary/reranking.md)
- [Vector database](https://skillalmanac.com/glossary/vector-database.md)

## Sources

- [Practical BM25 - Part 2: The BM25 Algorithm and its Variables](https://www.elastic.co/blog/practical-bm25-part-2-the-bm25-algorithm-and-its-variables), Elastic (accessed 2026-11-24)
- [Introducing Contextual Retrieval](https://www.anthropic.com/news/contextual-retrieval), Anthropic (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-24. [Glossary index](https://skillalmanac.com/glossary.md).
