Natural Language Processing · Intermediate level

Intermediate level · Module 8: Information Retrieval and Information Extraction · Lesson 1 of 3

Building a Document Search Engine

Learners build a search engine over public documents: an inverted index, TF-IDF cosine ranking and a hand-written BM25 scorer, with evaluation on test queries.

In this lesson you will

  • Build an inverted index and rank documents
  • Evaluate search results with precision@k

Key concepts

inverted indexqueryBM25 (k1, b)precision@krelevance judgements

The lesson

An inverted index maps each term to the documents that contain it, so a query only scores documents that share at least one term. A Python dictionary of sets is enough for a few thousand documents.

TF-IDF cosine similarity between the query and documents is a simple ranking. BM25 is the standard improved formula: it limits the effect of repeating a term (parameter k1, often 1.2–2.0) and adjusts for document length (parameter b, often 0.75). It can be written in about 15 lines of Python.

Evaluate with a set of test queries and relevance judgements: for each query, people mark which documents are relevant. precision@5 is the share of the top five results that are relevant.

Step by step

  1. Build an inverted index for the project corpus.
  2. Implement TF-IDF ranking and BM25 ranking.
  3. Write 10 test queries and mark the relevant documents.
  4. Compare precision@5 for both rankers.

Practice activity

Build and evaluate a search engine over the project's public documents.

Expected result: A working search with BM25 and TF-IDF compared on precision@5 for ten queries.

Check your understanding

  1. What does an inverted index store?

    Show answer

    For each term, the documents that contain it.

  2. What does BM25's b parameter control?

    Show answer

    How much scores are adjusted for document length.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.