Building with AI

BM25

Quick explainer2 min readIntermediate

BM25 is a classic formula search engines use to rank documents by how well their words match a query, and it is still the default in tools like Elasticsearch.

In plain words

BM25 gives each document a score. A document scores higher when the query's words appear in it often, but repeats count for less and less, so stuffing a word does not help much. Rare words across the collection count for more than common ones. Long documents are adjusted so they do not win just by containing more words. Two settings, k1 and b, control those effects.

A quick example

Imagine searching recipes for "saffron rice". Many recipes contain "rice", but few contain "saffron", so BM25 rewards the saffron match more. A short recipe mentioning both words ranks above a long cookbook chapter that mentions them once in passing.

Where you'll meet it

  • Keyword search in Elasticsearch, Lucene and Weaviate.
  • The keyword half of hybrid search, next to vector search.

Related

Sources

  1. DocsSimilarity settings, Elastic documentation · read 29 Sept 2026
  2. DocsKeyword search, Weaviate documentation · read 29 Sept 2026
  3. DocsBM25Similarity (Lucene 9.11.0 core API), Apache Lucene · read 29 Sept 2026

Checked by WhatIsAI against the sources above · 29 Sept 2026. Spotted an error? Report it.