We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Inverted Index

An inverted index is what makes keyword search fast. It's like a SQL database index, but for text. Instead of scanning every document on every search, we build a lookup table ahead of time.

A "forward index" maps location → value. An "inverted index" maps value → location.

Here's an example mapping words to the document IDs where they appear:

  • matrix[1, 5, 10]
  • hacker[1, 8]
  • reality[1, 3, 7]

Using an inverted index is fast: each token lookup is O(1) on average. Building the index is slower because we still have to read and tokenize every document once.

Assignment

Implement an InvertedIndex class and use it for keyword search.

    1. f"{m['title']} {m['description']}"
      
    1. print(f"First document for token 'merida' = {docs[0]}")
      

      We'll hardcode this as a very simple test to ensure that the index is built correctly.

Run and submit the CLI tests.