Search & indexing systems
How inverted indexes and relevance scoring power search, and why search lives beside your database, not in it.
Databases answer 'give me the row with this key'. Search answers 'which documents best match these words'. The core structure is an inverted index: for every term, a list of the documents that contain it, plus positions and counts. A query looks up each term, intersects or unions the lists, then ranks the matches.
Ranking uses statistics such as BM25, which rewards terms that are frequent in a document and rare across the corpus, and normalises for document length. Text is analysed first: lowercased, split into tokens, stripped of stop words and reduced to stems, so that 'running' can match 'run'. The same analysis must apply at index and query time.
Search engines are usually a derived copy of your primary data, kept in sync by change events. That makes search eventually consistent: a new record may take a second or more to appear. Plan for it, rebuild the index from the source of truth when the schema changes, and shard the index across nodes with replicas for availability, just as you would any other store.
Key points
- An inverted index maps terms to documents, which is what makes text search fast.
- Relevance comes from term statistics such as BM25, not just matching.
- Analysis must be identical at index time and query time.
- The search index is a derived, eventually consistent copy of your source data.
Answer these out loud
- 1A user searches 'color' and misses documents containing 'colour'. What in the analysis chain would you change?
- 2Describe how a product edit reaches the search index and how long the user may see the old title.
Build it
Build a tiny inverted index in 80 lines with tokenising, BM25 scoring and top-k results, and compare it to a SQL LIKE query on 100k documents.
Use this from an agent
Any MCP client, including Grok, can read this lesson. See how to connect.
curl https://engineeros-delta.vercel.app/api/v1/topics/search-and-indexing-systems