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.
Document 5 ranks first, with 3 of 3 query words.
Ranking
| Rank | Document | Words | Score |
|---|---|---|---|
| 1 | Document 5Track the hit rate, the eviction count and the memory use… | 68 | 2.132 |
| 2 | Document 3A time to live, or TTL, sets how long a cache entry stays… | 32 | 1.997 |
| 3 | Document 1A cache stores copies of data close to the application so… | 37 | 1.064 |
| 4 | Document 4With write-through caching, every write goes to the cache… | 30 | 0.341 |
| 5 | Document 2Cache invalidation removes or refreshes stale entries. Ti… | 24 | 0.272 |
| 6 | Document 6A CDN caches static files at servers near users. | 9 | 0 |
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.
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
| Word | idf | Count in doc (tf) | Length factor (K) | TF saturation | Score |
|---|---|---|---|---|---|
| cache | 0.241 | 1 | 2.136 | 0.702 | 0.169 |
| miss | 0.693 | 1 | 2.136 | 0.702 | 0.486 |
| rate | 1.03 | 4 | 2.136 | 1.434 | 1.477 |
| Document score | 2.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.
Query words
| Word | Documents with it (n of 6) | idf |
|---|---|---|
| cache | 5 | 0.241 |
| miss | 3 | 0.693 |
| rate | 2 | 1.03 |
Rarer words get a higher idf, so they move the ranking more.
Text is lowercased and split on anything that is not a letter or digit. There is no stemming, so “caches” and “cache” are different words, and no stop-word list. The idf is ln(1 + (N − n + 0.5) ÷ (n + 0.5)), the form Lucene uses: the “+ 1” keeps it above zero for very common words. Scores include the (k1 + 1) factor from the original paper; it multiplies every score equally, so the order does not change.
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- estimatorBack-of-envelope estimatorTurn daily users into requests per second, storage and bandwidth, with the working shown.Open tool
- simulatorConsistent hashing simulatorA hash ring with virtual nodes: key spread per node, and how many keys move when the cluster changes.Open tool
- estimatorQueue latency estimatorWhy latency explodes as servers get busy: utilisation, waiting time and Little’s law.Open tool