19. Search and Inverted Indexes
19.2 How do you tokenise and normalise text?
In the library: Before filing a book, the librarian decides which subjects it covers and writes them in a standard form: "Cooking", not "COOKING!!" or "cooked". Little words like "the" and "of" never get a card; every book has them.
In the system: An analyzer turns text into terms, the units that go into the index. A typical pipeline:
- Tokenise: split text into words. "Data-Intensive" becomes "data" and "intensive".
- Lowercase: "Databases" and "databases" become the same.
- Remove stop words (optional): drop very common words like "the", "of", "and".
- Stem or lemmatize: reduce words to a root, so "designing", "designs" and "design" all become "design".
Figure: the same analyzer runs on books when indexing and on queries when searching.
The golden rule: run the same analyzer at index time and at query time. If books are lowercased when indexed but the query "Database" isn't, nothing matches. That's exactly the Level 4 bug.
Other common steps, depending on the product:
- Synonyms: "DB" → "database", "sneakers" → "shoes".
- Removing accents: "café" → "cafe".
- Different languages need different analyzers. Stemming rules for English don't work for Hindi or Tamil, and some languages don't put spaces between words.
- Typo tolerance: usually done at query time with fuzzy matching (allowing one or two letter edits), not in the analyzer.
Memory code: "Clean the words the same way going in and coming out."