Ranking: TF-IDF and BM25
We give matching documents a comparison score. Raw word counts reward repetition and ignore whether a word is common. We will derive TF-IDF intuition, BM25 saturation, length normalization, and field-aware ranking.
#Section 13Rare terms carry more evidence
Every report could contain match, while only a few contain a team name. Matching the team name tells us more about the user’s intent. Document frequency counts how many documents contain a term. Inverse document frequency, or IDF, gives rarer terms more weight.
A simple TF-IDF variant uses , where is the number of documents and is document frequency. Read that as the logarithm of collection size divided by the number of documents using the term. In our corpus, red appears in 2 of 4 documents, giving . A term appearing in all documents would receive zero under this particular variant.
TF-IDF combines term frequency inside a document with inverse document frequency across the corpus. It is a family of formulas rather than one uniquely specified score. State normalization and smoothing before comparing numbers. Rarity measures discrimination under the text representation, not factual accuracy or trustworthiness.
#Section 14BM25 limits the benefit of repetition
Document 4 repeats bird, while documents 1 and 2 use it once. Rewarding repetition linearly lets a spammer repeat a term until a poor document dominates. BM25 is a relevance scoring family that gives diminishing returns for repetition and adjusts for document length.
For one term, use this stated illustrative form:
Read the first factor as a smoothed rarity weight. The second factor rewards term frequency while its denominator grows with frequency and a length adjustment. Here controls saturation, controls the strength of length normalization, is document length, and is average document length. These are named tuning values, not measured truths about language.
All four tiny reports have length 3, so average length is 3. With and , the length term becomes 1. For bird, gives IDF . One occurrence contributes , while two contribute . Doubling occurrences increases the score by less than a factor of two.
Lucene's BM25 API documents its parameter meanings and IDF form. Scores in this chapter are calculated with the explicit formula above, so they can be reproduced independently of a deployed engine version.
#Section 15Length normalization compares context
A long report has more opportunities to mention bird once than a short title does. BM25's length adjustment counters that tendency. For a document twice the corpus average length, the adjustment is . The denominator grows, reducing the same term-frequency contribution.
This correction is an assumption about how length relates to relevance. A long match report may be useful precisely because it explains more. Setting close to zero reduces the length penalty. Setting it close to one makes the document-to-average length ratio more influential. Tune using representative judgments, not a claim that a parameter is universally optimal.
A multi-term score commonly sums the contributions from matched terms. For document 1 and query red bird, the stated formula gives . This arithmetic is meaningful only under the stated term statistics and query construction. Query boosts, field normalization, and implementation choices can change a real engine's result.
#Section 16Relevance needs evaluation
Suppose the search returns five reports and a human judges three useful. Four useful reports exist in the collection for that question. Precision is useful returned results divided by all returned results, here . Recall is useful returned results divided by all useful results, here .
Changing an analyzer can increase recall by finding variants, but may reduce precision by merging unrelated meanings. A ranking change can move useful reports upward without changing membership. Keep a judged query set with difficult names, common words, spelling errors, and private content. A click log is informative but biased by what users were shown and where it appeared.
NDCG, normalized discounted cumulative gain, evaluates graded usefulness with more credit for earlier ranks and normalization against an ideal ordering. State the gain convention before calculating it. The study script records rank discounts, but we do not manufacture a quality score without a judged result ordering.