System Designcalculator

BM25 score breakdown

BM25 is the formula behind most keyword search. For each query word it asks two questions: how rare is the word across all documents, and how often does it appear in this one? Rare words count more, and repeats help less and less.

Top match

Document 5 ranks first, with 3 of 3 query words.

Top score2.132Document 5

Ranking

Documents ranked by BM25 score, best first
RankDocumentWordsScore
1Document 5Track the hit rate, the eviction count and the memory use…682.132
2Document 3A time to live, or TTL, sets how long a cache entry stays…321.997
3Document 1A cache stores copies of data close to the application so…371.064
4Document 4With write-through caching, every write goes to the cache…300.341
5Document 2Cache invalidation removes or refreshes stale entries. Ti…240.272
6Document 6A CDN caches static files at servers near users.90

Average document length: 33.3 words.

Your query and documents

Repeated words count once.

Documents

Edit any document, add one or remove one. Up to 12 documents of 2,000 characters.

37 words
24 words
32 words
30 words
68 words
9 words

How fast repeats stop helping. Each term’s score is capped at k1 + 1 times its idf. Lucene’s default is 1.2.

How much document length counts: 0 ignores it, 1 scales fully by it. Lucene’s default is 0.75.

Working for one document

Score working for document 5, one row per query word
WordidfCount in doc (tf)Length factor (K)TF saturationScore
cache0.24112.1360.7020.169
miss0.69312.1360.7020.486
rate1.0342.1361.4341.477
Document score2.132

K = k1 × (1 − b + b × words ÷ average words) = 1.2 × (1 − 0.75 + 0.75 × 68 ÷ 33.3). TF saturation = tf × (k1 + 1) ÷ (tf + K). Score = idf × saturation.

What it shows

Search engines rank documents by two questions. How rare is each query word across all documents? How often does it appear in this one? Rare words count more, and repeats help less and less.

The tool scores the documents you give it against your query and shows the working for each one. Change k1 and b, or edit the text, and watch the ranking move.

How to read it

  • Query words lists how many documents contain each word, and its idf. A word in almost every document gets an idf near 0.
  • Ranking orders documents by score. Pick one document to see a row per query word: its count (tf), length factor K, TF saturation and score.
  • k1 sets how fast repeats stop helping. The score for a word can never pass k1 + 1 times its idf.
  • b sets how much document length counts. At 0 length is ignored; at 1 it scales fully.
  • On the chart, the dashed line is a document twice the average length. The ceiling marks k1 + 1.
  • Words are lowercased and split on anything that is not a letter or digit. There is no stemming.

Questions

Why is the idf never negative here?

This tool uses the form Lucene uses: ln(1 + (N − n + 0.5) ÷ (n + 0.5)). The “1 +” keeps it above zero even for a word that is in every document. The original Robertson–Sparck Jones weight can go negative for words in more than half the documents.

Will a search engine give the same numbers?

The shape is the same, but engines differ in detail. Lucene’s defaults are k1 = 1.2 and b = 0.75, as used here. Real engines differ in details such as scoring per field and how they store document length. This tool keeps the (k1 + 1) factor from the original paper; it multiplies every score equally, so it never changes the order. Treat the scores as a way to see the mechanism, not to predict an engine’s exact output.

Why does a short document sometimes lose to a long one?

Length only changes how much each repeat is worth. A long document that contains the rare query words can still beat a short one that contains only the common ones, as document 5 does in the example. Raising b makes length matter more.

Can I compare scores across different queries?

No. The scores depend on the idf of the query words, so a score of 2 for one query is not worth the same as a score of 2 for another. Compare documents only within one query.

Learn it properly

Sources

Explore further

  • bm25sFast BM25 search in Python, for scoring a real document collection.

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.