build-a-search-engine / lesson-01.md
Lesson 01 · Documents & analysis

The index and its documents

Today you create the core object of the whole project - a search index you can add documents to and read them back. Everything else you build hangs off this one type.

The goal

Build a SearchIndex that stores documents by id and reports how many it holds.

Start here - the target
TO DO
Scenario: Adding and retrieving documents
Givena new empty search index
Whenyou add ("d1", "hello world") and ("d2", "goodbye")
Thenindex.size is 2
Andindex.document("d1") is "hello world"
Background

Every search engine begins with the same humble object: a place to put documents. Before you can tokenize, index, or rank anything, you need a document store - a mapping from a document id to its original text. Keeping the raw text around matters: later you will show snippets of it in results, so the store is not just a stepping stone.

Keep it tiny. A dictionary from id to text, an add method, a size, and a document(id) lookup. This is the spine the rest of the project attaches to - every later lesson adds a method to this same class.

Make it work
class SearchIndex:
def __init__(self):
self._docs = {} # id -> original text
def add(self, doc_id, text):
... # store it
# size, document(id) read it back
Further Reading

Manning, Introduction to Information Retrieval - ch. 1.

CheckpointDONE
You can add documents to an index and read them back by id. Commit and stop here.