Search & the Inverted Index
You'll learn to
- -Explain why scanning rows with LIKE cannot scale to text search
- -Build an inverted index by hand and answer a two-word query by intersecting lists
- -Explain relevance ranking (TF-IDF intuition) and the two problems of search: finding candidates, and ranking them
- -Describe how a search index sits next to the primary database, and why its results can lag
Sooner or later every product needs a search box: find the product, the post, the document that contains these words. The obvious approach, asking the database for every row that contains the text, works with a thousand rows and collapses at a hundred million. Search needs a different data structure, built for exactly this question, and it is one of the most elegant ideas in this course.
The Problem: Finding Text Fast
A SQL query such as LIKE '%term%' has to look inside every row for the substring. Cost grows with the size of the table, and a normal database index cannot help, because the text could match anywhere inside the value. At a hundred million rows each search reads a hundred million rows.
An Analogy: The Index at the Back of a Textbook
When you want to find where a textbook discusses replication, you do not read the whole book. You open the index at the back, find the word, and read off the page numbers. The book is arranged page to words, but the index is the other way round: word to pages. That reversal is the whole idea.
- -The pages of the book are the documents.
- -The word you look up is a term.
- -The list of page numbers beside it is the posting list for that term.
- -The index at the back, as a whole, is the inverted index: it maps each word to the documents containing it.
Building One by Hand
Take three tiny documents: 1 is distributed systems scale, 2 is distributed databases, and 3 is systems design. Going through each document and recording where every word appears gives this inverted index: distributed maps to documents 1 and 2, systems to 1 and 3, scale to 1, databases to 2, design to 3. To answer the query distributed systems, look up the two posting lists, [1, 2] and [1, 3], and keep the documents that are in both. The answer is document 1, found without reading a single document. The sketch below does exactly that:
The cost of a query now depends on the length of the posting lists involved, not on the size of the whole collection, which is why search engines can answer in milliseconds over billions of documents.
Before Indexing: Preparing the Text
Real engines clean the text first, so that searches match what people mean. They lowercase everything, split it into words, drop very common words (such as the and of) that match everything, and reduce words to a root so that running, runs and ran match run. The same preparation is applied to the query, otherwise the words would not line up.
The card below describes the search index component as it appears in the lab:
Imagine you have 10 million products in your store. A customer types 'red runing shoes' (with a typo!) into the search bar. A regular database would do a simple text match and find nothing. There's no product called 'runing.' A search index, powered by technology like Elasticsearch, is built specifically for this. It creates an inverted index (like the index at the back of a textbook. Instead of page->words, it maps word->documents). It handles typos (fuzzy matching: 'runing' -> 'running'), synonyms ('sneakers' also shows 'running shoes'), relevance ranking (most popular red running shoes first), faceted filtering (filter by size, brand, price range), highlighting (shows WHERE the match occurred in the text), and autocomplete ('run...' suggests 'running shoes, running shorts, running watch'). All of this in milliseconds, across millions of documents.
Examples: Elasticsearch (the dominant player), OpenSearch (AWS-managed fork), Solr, Typesense (lightweight, easy to run), Meilisearch (developer-friendly), Algolia (managed search-as-a-service)
Ranking: Which Match Comes First?
Finding the matching documents is only half of the job. A query can match thousands, and users read the first few. So search engines also score each match for relevance. The intuition behind the classic measure, TF-IDF, is two ideas multiplied together. A word matters more in a document if it appears often there (term frequency), and matters more if it is rare across all documents (inverse document frequency). The word the appears everywhere and tells you nothing; the word inverted appears in few documents, so finding it in one is a strong signal. BM25, the modern default, refines the same idea. This is why designing a search engine is really two problems: find the candidates quickly, and rank them well.
Where It Fits in a System
A search index is not your primary database. The standard design keeps the database as the source of truth and feeds a separate search system (Elasticsearch and OpenSearch are common) through an asynchronous pipeline, so search queries never touch the production database. The diagram shows the flow:
Writes go through the primary database as usual; a separate pipeline keeps the search index updated so search queries never touch the OLTP database directly.
- -Results can lag: a new document is searchable after the pipeline has processed it, usually seconds later. This is eventual consistency, and it is normally acceptable for search.
- -Never treat the search index as the only copy of your data: it can be rebuilt from the database, and sometimes has to be, when the text preparation rules change.
- -At scale the index is split into shards by document and each query is sent to every shard, then the best results are merged. Replicas add read capacity and survive a failure.
In one line: an inverted index maps each word to the documents containing it, so a query is a few list lookups and an intersection. Add relevance ranking, keep the index beside the database and fed asynchronously, and accept that results can lag by a moment.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!