SD System Design In Depth
0%
lesson system-designsearch

TF-IDF relevance scoring

Module 5.4: Information Retrieval & Search Engines Updated Sep 17, 2026
📄 Cheat Sheet
8 7 Calculating TF IDF Cosine Scores 12 47
From Languages to Information 8 7 Calculating TF IDF Cosine Scores 12 47 Watch on YouTube ↗

Manning computes log-TF, IDF and cosine scores by hand on a real example, matching this unit’s exact formulas.

0:00 / --:--

The Foundations of Lexical Relevance

How does a search engine decide whether Document A is more relevant to a query than Document B? The foundational mathematical model of information retrieval is TF-IDF (Term Frequency - Inverse Document Frequency).

The Two Core Components of TF-IDF

flowchart LR TF["Term Frequency (TF): How often does term t appear in doc d? (Local Importance)"] --> Mult["Multiply: TF x IDF"] IDF["Inverse Document Frequency (IDF): How rare is term t across all N docs? (Global Discriminator)"] --> Mult Mult --> Score["Relevance Weight for (t, d)"]

Under the Hood: The Mathematical Formulas

1. Term Frequency (TF)

Measures how frequently term t occurs in document d. The simplest form is raw count freq(t, d), but modern systems apply logarithmic damping to prevent documents with 100 mentions of a word from dominating documents with 10 mentions:

TF(t, d) = 1 + ln(freq(t, d))  (for freq > 0)

2. Inverse Document Frequency (IDF)

Common words (e.g. "the", "system") appear in almost every document and provide zero discriminatory power. Rare words (e.g. "Paxos", "Bloom") carry high information entropy. Given total documents N and document frequency DF(t):

IDF(t) = ln(1 + (N / DF(t)))

3. The Vector Space Model (Cosine Similarity)

Documents and queries are represented as high-dimensional vectors in term space. The relevance score is computed as the cosine of the angle between query vector q and document vector d:

Cosine_Similarity(q, d) = (q . d) / (||q|| * ||d||)

Worked example: why the log matters

Corpus: N = 1,000,000 documents. Query: raft system.

TermDFIDF = ln(1 + N/DF)
raft1,000ln(1001) = 6.91
system400,000ln(3.5) = 1.25

Document A mentions raft 3 times and system 10 times. Document B mentions raft once and system 30 times.

ScoringDoc ADoc BWinner
Raw TF x IDF3 x 6.91 + 10 x 1.25 = 33.31 x 6.91 + 30 x 1.25 = 44.5B
Log TF x IDF, TF = 1 + ln f2.10 x 6.91 + 3.30 x 1.25 = 18.61.00 x 6.91 + 4.40 x 1.25 = 12.4A

With raw counts, B wins by repeating a near-useless word. With log damping, A wins because it talks more about the rare, discriminating term. This is the core lesson of TF-IDF.

Cosine normalisation in one line

Dividing by vector length means a 10,000-word document is not rewarded simply for containing more terms. In practice engines precompute each document's norm at index time so query-time cost stays proportional to the query's postings.

TF variants compared

TF formBehaviourUsed in
Raw countLinear, easily gamed by repetitionTeaching examples
1 + ln fGrows slowly but without boundClassic SMART lnc.ltc schemes
sqrt fSub-linearLucene ClassicSimilarity, the pre-BM25 default
BM25 saturationBounded, tunable, length-awareDefault in Lucene 6 and later, Elasticsearch 5 and later

Failure modes: IDF computed per shard can differ across shards when documents are unevenly distributed, which is why Elasticsearch offers a dfs_query_then_fetch mode that gathers global term statistics first; tiny corpora produce noisy IDF values.

Interview framing

If asked "how would you rank documents for a keyword query?", start with TF-IDF to show you understand the two forces, local frequency and global rarity, then say production engines use BM25 because it bounds repetition and normalises length with tunable parameters, and finally mention a learned re-ranker on top for non-text signals such as popularity and freshness. That three-step answer covers the evolution from 1970s vector space models to modern two-stage ranking.

Check yourself Question 1 of 3
Role of IDF recall
From: Under the Hood: The Mathematical Formulas

In a corpus of 10 million documents, the term system appears in 9 million of them and the term paxos in 2,000. What does the IDF factor do to their weights?

BM25 Search Ranking
Information Retrieval

Key Takeaways

TF measures how often a term appears in a document; logarithmic damping prevents term-stuffing manipulation.
IDF penalizes ubiquitous common words and boosts rare, highly informative keywords.
The Vector Space Model computes document relevance via cosine similarity between query and document vectors.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue