BM25, Best Matching 25, is a probabilistic ranking function used in information retrieval to score how relevant a document is to a query. It is a refined version of TF IDF and is widely used in search engines and classical retrieval systems

Before reading this and applying this you need to know about TF-IDF and Inverted Index.

Why it exists?

IDF Problem

IDF is calculated as log(N + 1 / df + 1)

The problems with this formula:

BM25 solved this by comparing the documents that don’t contain the term against documents that do contain it:

$$ IDF(t) = \log \frac{N - df + 0.5}{(df + 0.5) + 1} $$

Term Frequency Saturation