build-a-search-engine / lesson-22.md
Lesson 22 · Ranking

Scoring a multi-term query

Today you score a document against a whole query by summing its TF-IDF weight for each query term. This is the vector space model's answer to "how well does this document match these words?"

The goal

Score a document for a multi-term query as the sum of its tf-idf weights over the query terms.

Start here - the target
TO DO
Scenario: Adding up per-term weights
Givena document and a query - the query score is the sum of the document's tf-idf weight for each query term
Whenthe document's weights happen to be 4.0 for "cat" and 1.5 for "dog"
Thenthe score for the query "cat dog" is 4.0 + 1.5 = 5.5
Anda query term the document lacks contributes its weight of 0.0
Background

A query is more than one word, so a document needs one score for the whole thing. The vector space model’s answer is simple: add up the document’s tf-idf weight for each query term. A document strong on cat and also present on dog outscores one that only has cat, and query words the document never uses contribute their weight of zero.

This summation is what turns per-term weights into a ranking signal. It quietly favors documents that match more of the query, since each matched term adds another positive weight - a reasonable default that the next lessons refine by correcting for document length.

Make it work
def query_score(self, terms, doc_id):
return sum(self.weight(t, doc_id) for t in terms)
# weight(t, doc) = tfidf(tf(t, doc), idf(N, df(t)))
Further Reading

Manning, Introduction to Information Retrieval - ch. 6.3.

CheckpointDONE
A document earns one score for an entire query. Commit and stop here.