AI

WritingSearch series

Text Analysis

Before a word can be indexed, it has to be prepared. The quiet pipeline that decides what your search can ever match.

By Sachin Gupta7 min read
Portrait of Sachin Gupta rendered in binary

Part of the search series. The inverted index does not store raw text; it stores terms, and text analysis is the pipeline that turns one into the other: tokenize, lowercase, drop stopwords, stem to a root, expand synonyms. The same analyzer must run on the query, and getting it wrong silently breaks search. With an interactive analysis pipeline.

You do not throw whole, unwashed vegetables into a pot. You rinse them, peel them, and chop them into pieces of a consistent size first, because that is what makes them cook evenly. Text analysis does the same to raw text before it is indexed: it takes a messy sentence and turns it into clean, consistent pieces a search engine can actually work with.

In How Search Works the inverted index mapped a word to the documents that contain it. But it does not store the raw word. Between the document and the index sits an analyzer, a small pipeline that decides what a "term" even is. That pipeline quietly sets the ceiling on everything search can match, and most search bugs are really analysis bugs.

The text-analysis pipeline: raw text is tokenized, lowercased, stripped of stopwords, and stemmed to root forms, turning a sentence into the handful of terms that get indexed. The same pipeline runs on the query

Tokenization

First the text is broken into tokens. "The cats are running." becomes The, cats, are, running. That sounds trivial until you meet punctuation, hyphens, URLs, C++, or languages that do not put spaces between words. The tokenizer is where those decisions get made, and a bad one can split e-mail into e and mail or lose C# entirely.

Normalization

Next the tokens are made uniform. Lowercasing is the obvious one: without it, Apple and apple are two different terms and a search for one misses the other. Analyzers also fold accents (café to cafe), strip diacritics, and normalize width and punctuation, so that trivial surface differences do not become missed matches.

Stopwords

Some words carry almost no signal. the, a, is, to, of appear in nearly every document, so matching them tells you nothing and they cost space in every postings list. Classic search drops these stopwords entirely. Modern engines are often more careful (removing the from "The Who" or "to be or not to be" destroys the query), but the idea, spend the index on words that discriminate, is still everywhere.

Stemming and lemmatization

Here is the one that matters most for recall. A user who searches run expects to find documents that say running, runs, or ran. Left alone, those are four different terms that never meet. Stemming chops words down to a rough root with fast rules: running to run, cats to cat, quickly to quick. It is crude and sometimes over-reaches (universe and university can collapse together), but it is cheap and it works. Lemmatization is the careful cousin: it uses a dictionary and part-of-speech to find the real base word, turning better into good and ran into run, at more cost. Either way, the point is to make different forms of the same idea land on the same term.

Synonyms

The last common step teaches the index that different words mean the same thing. A synonym filter expands or rewrites tokens so that NYC also matches New York, and heart attack also matches myocardial infarction. It is how a search stops depending on the user guessing the exact word your documents happened to use.

The one rule that breaks everything

Analysis runs in two places, and they must agree. The same analyzer that processed the document at index time has to process the query at search time. Index Running as run but search for the raw word running, and you get nothing, because running is not a key in the index; run is. Almost every "why does my search return zero results" bug is a mismatch between the index-time and query-time analyzers.

Watch a sentence go through the whole pipeline and turn into the terms that actually get stored:

Interactive · The text-analysis pipelineOpen in Visualizers →
Type a sentence
1 · Tokenize
TheCatsareRunningquicklytoNYC
2 · Lowercase
thecatsarerunningquicklytonyc
3 · Remove stopwords
thecatsarerunningquicklytonyc
4 · Stem → index terms
catcatsrunrunningquickquicklynyc
The dark chips are what actually gets indexed. The same pipeline must run on the query too, so “Running” in a document and “run” in a search still meet as “run”.

Analysis is unglamorous and it decides everything. With clean terms in the index, the next question is how to rank the documents that match them, which is where BM25 comes in.

Related

Tagged