Search: the inverted index and the reindex you owe
Somebody asks for a search box. The first implementation is WHERE title LIKE '%term%', and it works on the laptop dataset. It cannot survive contact with production for two independent reasons, and the second one is the interesting half.
The mechanical reason: a leading wildcard cannot use a B-tree index (Lesson 3.02 — indexes are sorted by prefix), so every query is a full scan. The real reason: matching is not the requirement. A thousand rows contain the word "charger". The user wants the five they meant. Search is a ranking problem wearing a filtering problem's clothes, and no amount of SQL cleverness turns one into the other.
Inverting the map
A normal index answers "what is in this document". A search index answers the inverse: for a term, which documents contain it, where, and how often. That is an inverted index — a dictionary of terms, each pointing at a posting list of document IDs. Intersecting two short posting lists is fast and scales to hundreds of millions of documents. The entire field is built on this one inversion.
Analysis is where quality is decided
The index does not store your text, it stores terms, and the pipeline that produces them is the single biggest determinant of whether search feels good:
- Tokenise — split into terms, which is a language question. Whitespace works for English and fails for compound German nouns, for Chinese, and for
iphone-15-pro. - Normalise — lowercase, strip accents, fold punctuation, so
Caféandcafemeet. - Stem or lemmatise —
running,ranandrunscollapse to one term, so a query matches a document that never used its exact word. - Synonyms and n-grams — mapping
laptoptonotebook, and character n-grams for typo tolerance and substring matching.
Two consequences follow immediately. The query text must go through the same analyzer as the documents, or you will search for a term that cannot exist in the index. And analysis is applied at write time, which means changing your mind about any of it invalidates every document you have already indexed. Hold that thought.
Relevance is a product decision
The textual score — BM25 in most engines — rewards documents where the term is frequent and the document is short, and discounts terms that appear everywhere. It is a good default and it is never the whole answer, because "best match" in a real product blends text with signals the text does not carry: popularity, recency, whether the item is in stock, the user's own history. That blend is a business decision that engineers habitually leave at the library default and then wonder why the results feel wrong.
Two mechanics worth carrying: filters and queries are different things — a filter is boolean, cacheable, and does not contribute to score, so structured constraints belong there rather than in the scoring clause. And permission filtering belongs inside the query. Retrieving the top fifty and then removing what the user may not see ranks over documents they cannot have, returns too few results, and leaks the existence of the rest.
The index is a projection, and that means a reindex
Everything in Tier 7 applies: the search index is derived data. It is a lossy copy of documents owned elsewhere, it lags behind the source, it can be rebuilt from scratch, and it must never be the source of truth. What makes it distinctive is how often the rebuild is compulsory. Add a field, change an analyzer, alter a mapping, tune a language setting — every one of those changes applies to all eighty million existing documents, and none of them can be applied in place.
So the reindex path is part of the design, not an incident you improvise. The mechanism is Lesson 9.05's expand–migrate–contract with a search flavour: build a second index alongside the live one, backfill it from the source of truth while dual-writing every live change into both, compare counts and sample queries, then flip an alias — the pointer your application queries — atomically, and keep the old index around until you are confident. A team that has never done this has an index they cannot change.
Two operational facts that shape designs. In a sharded index, a query is scatter-gather, so your p99 is the slowest shard, and adding shards makes tails worse rather than better. And deep pagination is quadratic: page 500 requires every shard to produce 5,000 results so the coordinator can merge and discard them. Cap the page depth, and use a cursor (Lesson 1.05) or "refine your search" instead.
Promoting the search engine to primary datastore because it holds all the fields anyway. It has no multi-document transactions, its consistency story under partition is weaker than your database's, and its "real time" is a refresh interval — a document written a moment ago is legitimately not findable yet. Systems that made this move discover it during the first reindex, when the only complete copy of the data is the thing being rebuilt.