Introduction
By Ludovico Bessi, Senior ML Engineer at Google · Updated
Search in the AI era is the problem of finding the right documents for a query when the "user" may be a person scrolling results or an LLM that will read them and reason. The core funnel — sparse and dense retrieval followed by reranking — is the same, but what you optimize for changes: sometimes raw latency, sometimes maximum precision for a model that will think for a minute anyway.
Search in the AI Era?
Search used to have one customer: a human typing a few words and scanning ten blue links. Now there are at least three:
- A human, interactively. Results must stream in fast. Typeahead and instant results need answers in tens of milliseconds.
- An LLM in a chat product (RAG). The model reads the top results and writes an answer. A human is still waiting, so latency matters, but the model reads every result you give it — irrelevant results now actively hurt, because they become context the model can be misled by.
- A deep-thinking LLM or agent. A research agent may issue dozens of searches and think for minutes. Here a few hundred extra milliseconds per search are irrelevant; what matters is getting the right documents at all. Maximum precision and recall, even at a much higher cost per query.
So the first question when designing a search system is not "which embedding model?" It is: what do you optimize for?
What Do You Optimize For?
- Latency. p50 and p99 per query. Streaming UIs and typeahead need very fast retrieval; agent tools can be slow.
- Recall. Did the relevant document make it into the results at all? For an LLM consumer this is usually the most important number, because a missing document cannot be recovered downstream.
- Precision. How much of what you return is relevant? For a human, low precision is annoying. For an LLM, it is noise in the context window, and you pay for every token.
- Engagement (clicks, dwell time, conversions). For consumer products with human users, clicks are the cheapest large-scale signal of relevance. They are biased (people click the top result regardless), but they are abundant.
- Cost. GPU time per query, index memory, and LLM tokens for reranking or for the downstream model.
These trade off against each other. Most of this book is about moving along that trade-off curve deliberately rather than by accident.
Classic Ways to Do Search
Sparse retrieval
The classic approach: build an inverted index mapping each term to the documents containing it, and score with BM25, which rewards rare terms that appear often in a document, normalized by document length. It is fast, cheap, needs no training, and is excellent at exact matches — names, product codes, error messages. It fails on paraphrases: "how to cancel my plan" does not match "terminate subscription".
Dense retrieval
Embed queries and documents with a neural model into vectors so that semantically similar texts are close. Retrieval becomes nearest-neighbor search. It handles paraphrase and meaning, works across languages, and is the foundation of modern semantic search. It can miss exact rare tokens and needs good training data. Chapter 3 covers how to train these models.
Reranking
Retrieval is a cheap pass over everything. Reranking is an expensive pass over a few hundred or thousand candidates, with a model that reads the query and each document together (a cross-encoder or an LLM). It is where most of the precision comes from.
The Funnel
Put together, nearly every modern search system looks like this:
corpus: millions → billions of documents
│
┌───────────────────────┴───────────────────────┐
sparse retrieval (BM25) dense retrieval (embeddings)
top-K ≈ 1,000 top-K ≈ 1,000
└───────────────────────┬───────────────────────┘
merge / score fusion
│ ≈ 1,000 candidates
reranker (cross-encoder / LLM)
│ top 10–50
human results page │ or LLM context window
Each stage is more expensive per document and sees fewer documents. The art is choosing how many candidates flow between stages, and how much compute each stage gets, given what you optimize for.
How This Book Is Organized
- Search Infrastructure — how nearest-neighbor search actually works: brute force on GPUs, branch and bound, locality-sensitive hashing, and graph algorithms like HNSW.
- Neural Retrieval & Ranking — the modelling side: BM25, training embedding models, hybrid scoring, bi-encoders vs. cross-encoders vs. ColBERT, Matryoshka embeddings and quantization.
- Case Studies — three archetypal designs with pseudocode, and a capstone on LinkedIn's LLM-based semantic search, which skips approximate search entirely.
For a RAG-focused treatment, see the companion book RAG Systems at Scale.
Key Takeaways
- Search now serves humans, chat LLMs and deep-thinking agents, each with different latency and precision needs.
- Decide what you optimize for — latency, recall, precision, engagement, cost — before choosing models.
- Sparse retrieval matches exact terms; dense retrieval matches meaning; reranking adds precision.
- Almost every system is a funnel: cheap retrieval over everything, expensive reranking over a few.