Quick revision
The short answer
BM25, the default similarity in Lucene, Elasticsearch and OpenSearch, scores each query term in a document by multiplying its inverse document frequency, which gives rare terms more weight, by a term frequency part that grows with more occurrences but saturates, controlled by k1 (default 1.2), and is lowered for long fields by length normalisation, controlled by b (default 0.75). The scores of all query terms are added up. Because the numbers depend on the index's statistics, scores are not comparable across different queries or indices.
What to remember
In this answer, we saw that BM25 scores each query term by multiplying inverse document frequency, which makes rare terms count far more than common ones, by a term frequency part that saturates under k1 = 1.2 and is reduced for longer fields by length normalisation with b = 0.75. Term scores are summed, the explain API shows the calculation, and because everything depends on index statistics, scores order results within one query but cannot be compared across queries or indices.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
BM25, the default similarity in Lucene, Elasticsearch and OpenSearch, scores each query term in a document by multiplying its inverse document frequency, which gives rare terms more weight, by a term frequency part that grows with more occurrences but saturates, controlled by k1 (default 1.2), and is lowered for long fields by length normalisation, controlled by b (default 0.75). The scores of all query terms are added up. Because the numbers depend on the index's statistics, scores are not comparable across different queries or indices.
Why this question is asked
Every time a search engine returns results in some order, a scoring formula decided that order. When relevance looks wrong, we cannot fix it without knowing what the score rewards. BM25 is that formula in Lucene-based engines, and interviewers ask about it because understanding its three parts explains most ranking surprises: why a short title beats a long description, why repeating a word stops helping, and why a rare word dominates. In this answer, we will recap what a score is, take the formula apart term by term with numbers, see how the parts combine, and look at what scores can and cannot be used for.
1A Quick Recap of Scoring
A full-text query such as match finds the documents that contain the query's terms through the inverted index, and then gives each match a score, a number that says how well it matches. Results are sorted by that score. The part of the engine that computes it is called the similarity, and since Lucene 6 and Elasticsearch 5 the default similarity is BM25, short for Best Matching 25, a formula that came out of information retrieval research.
BM25 scores each query term separately in each field and then adds the term scores. For one term in one document, it multiplies two factors: how important the term is in general, and how strongly this document is about it.
2Inverse Document Frequency: Rare Terms Count More
The first factor is IDF, inverse document frequency. It depends on N, the number of documents in the index (more precisely, in the shard), and n, the number of documents that contain the term. In Lucene's form it is ln(1 + (N - n + 0.5) / (n + 0.5)).
So a match on 'kafka' is worth more than six times a match on 'java'. The reason is intuitive: a word that appears everywhere tells us little about which document the user wants, while a rare word is a strong signal. Notice that the formula never goes negative thanks to the 1 + inside the logarithm, a change Lucene made to the classic BM25 for this reason.
3Term Frequency With Saturation
The second factor rewards documents that use the term more often, but with diminishing returns. Ignoring length for a moment, the term frequency part is tf / (tf + k1), where tf is how many times the term appears in the field and k1 is 1.2 by default.
| Occurrences | Average-length field | Field twice as long |
|---|---|---|
| 1 | 0.45 | 0.32 |
| 2 | 0.63 | 0.49 |
| 5 | 0.81 | 0.70 |
| 10 | 0.89 | 0.83 |
This is saturation: the second occurrence adds a lot, the tenth adds little, and the hundredth almost nothing. The older TF-IDF scoring grew with the square root of the frequency without a limit, so stuffing a keyword into a document kept raising its score. A larger k1 makes saturation slower, so frequency matters more; k1 = 0 ignores frequency entirely.
4Field Length Normalisation
A match in a short field means more than the same match in a long one. 'Kafka' in a five-word title says the document is about Kafka; 'Kafka' once in a 2,000-word article may be a passing mention. BM25 handles this by replacing k1 in the denominator with k1 x (1 - b + b x dl / avgdl), where dl is the field's length in terms and avgdl the average length of that field across documents.
The parameter b sets how strongly length counts: b = 0 ignores length, b = 1 normalises fully, and 0.75 is in between. Lucene stores field lengths in a compact one-byte form called norms, so lengths are approximate, and for fields where length means nothing, such as tags, we can disable norms in the mapping.
5Putting the Score Together
For one term the score is roughly idf x tf part, multiplied by any boost; for a query with several terms the engine adds the term scores, and for a multi-field query the query type decides how field scores are combined. Taking our numbers, a short document mentioning 'kafka' once and 'java' twice scores about 4.56 x 0.45 + 0.69 x 0.63, which is 2.07 + 0.43 = 2.50. The rare term contributes most of it, as intended. The explain API prints this calculation for any document, with each factor named, which is the quickest way to answer 'why is this result first?'.
Because the numbers depend on N, n and avgdl, a score of 2.50 has no absolute meaning. A different query, a different index, or even the same index after many updates produces different scales. So scores are good for ordering results of one query, not for comparing queries or setting a fixed 'relevance threshold' across indices.
6Follow-up questions
Should we tune k1 and b?
Rarely, and only with a judged query set to measure the effect. The defaults work well for most text. Short fields like titles sometimes benefit from a lower b; fields where repetition matters, like tags weighted by count, might use a different k1.
Why do scores differ between shards?
By default IDF and average length are computed per shard, so the same document can score differently depending on which shard holds it, especially in small indices. dfs_query_then_fetch computes global statistics at extra cost.
Is BM25 the only option?
No. Lucene offers other similarities, and fields can use a custom similarity, while function_score, rescoring and learning to rank change the final order on top of BM25.
7Conclusion
In this answer, we saw that BM25 scores each query term by multiplying inverse document frequency, which makes rare terms count far more than common ones, by a term frequency part that saturates under k1 = 1.2 and is reduced for longer fields by length normalisation with b = 0.75. Term scores are summed, the explain API shows the calculation, and because everything depends on index statistics, scores order results within one query but cannot be compared across queries or indices.
Open with
BM25 is the default similarity in Lucene, and therefore in Elasticsearch and OpenSearch. It scores each query term in each matching document and adds the term scores together.
Then, if they want more
Each term score has two main parts. The first is inverse document frequency: it depends on how many documents contain the term compared with the total, so a rare term like 'kafka' might get a weight of four and a half while a term in half the documents gets less than one. Rare terms carry more information.
The second part is term frequency, and it saturates: the formula is roughly tf over tf plus k1, with k1 defaulting to 1.2, so one occurrence gives about 0.45, two about 0.63, and ten only 0.89, never above one. Repeating a keyword stops paying off, unlike classic TF-IDF.
Inside that denominator is length normalisation, controlled by b, default 0.75: the k1 term is scaled by the field length relative to the average, so a match in a short title scores higher than the same match in a long body. b of zero ignores length entirely.
So the score is IDF times the saturated, length-normalised term frequency, summed across terms, times any boosts. Because it all depends on the index statistics, scores are only meaningful for ranking results of one query, not for comparing across queries or indices. The explain API shows the whole calculation.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Elasticsearch documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how BM25 scores a document for a query. What do term frequency, inverse document...": https://scrivio-interview-library.netlify.app/databases/search-engines/d6fce9e3af/