MMOOZZGG.com

Glossary

What is the BM25 algorithm

In short

The BM25 algorithm is a ranking formula for keyword search. It scores a document by how many times the query words appear in it, how rare those words are, and how long the document is.

Search and RAG · BM25

MMOOZZGG.com editorsChecked: 1 min read

For a child

Imagine you look for a book about giraffes in the class library. A book where the word “giraffe” appears many times is probably about them. The word “and” is in every book, so it helps you find nothing. Also, a thick book may mention a giraffe once by chance. BM25 counts all this and puts the right book first.

In plain words

BM25 is a classic way to search by matching words, without neural networks. For each document the formula counts points. When a query word appears several times, the document gets more points. But after a few repeats the gain almost vanishes, so a text stuffed with one word does not win. A rare word weighs more than a common one: “refund” matters more than “the”. A long document is slightly penalized, because words appear in it simply due to its size. For example, for a query with a cable model such as “VVGng 3x2.5”, this search finds the card of exactly that product at once.

For business

Keyword search is still needed in almost every system that answers from documents. It is exact where neural search makes mistakes: part numbers, contract numbers, surnames, error codes and rare industry terms. The method runs on an ordinary processor, needs no training and is built into common search engines, so it costs almost nothing. Its weak point is synonyms and rephrasing: it will not link “get my money back” with “request a refund”. For languages with many word forms, such as Russian, words must be reduced to a base form first, or the search misses grammatical variants. Teams usually put BM25 next to meaning-based search. For an online shop this pair covers both part number search and customers’ questions in their own words.

For a specialist

BM25 is a ranking function from the Probabilistic Relevance Framework, described in detail in the survey by Robertson and Zaragoza (2009). A term weight has two parts. The first part, with no relevance data, reduces to an IDF form and is higher for rare terms. The second part depends on term frequency in the document through a saturation function: the contribution grows with frequency but does not pass a limit, and the parameter k1 sets the steepness. Document length is normalized against the average length with strength b. The model does not say how to choose the parameters; according to experiments the authors cite, 0.5 < b < 0.8 and 1.2 < k1 < 2 suit many cases. Elasticsearch uses BM25 by default with k1 = 1.2 and b = 0.75. The BM25F extension accounts for document fields such as title, body and links. In RAG pipelines BM25 serves as the sparse retriever in hybrid search and as a baseline: Dense Passage Retrieval (Karpukhin et al., 2020) was compared with exactly this method. Russian text needs lemmatization or stemming before indexing.

Example

A manager searches the database for “invoice 1047 from Vector LLC”. Meaning-based search offers similar invoices of other clients, while BM25 puts the document with this number and name first.

Related terms

Questions and answers

Why use BM25 if you have meaning-based search? Meaning-based search is weak at part numbers, codes and rare terms, while BM25 finds them exactly. So systems that answer from documents usually use both together, in hybrid search.
Does BM25 work with Russian? Yes, if words are reduced to their base form or stem before indexing. Without this, two grammatical forms of the word “contract” count as different words, and some documents get lost.

Sources

Not sure whether your company needs this?

AI solutions map
Contact us