Part 1 · Foundations · Chapter 3
Search in one chapter
In one sentence: a search system builds an index so that it can find candidate documents fast, gives each a score for the query, and returns them best first, and researchers test it with a fixed set of questions and human judgments.
Why you should care
The “R” in RAG is a search engine. People who come to RAG from machine learning often treat it as a black box, and then cannot tell why their system fails. An hour on the basics of search, the field called information retrieval, pays back quickly.
Words you need
- Information retrieval (IR)
- The science of finding the documents that satisfy a need, in a large collection.
- Document
- One unit that can be returned: a web page, a passage, a news article.
- Query
- What is sent to the search system. It may be shorter than, or different from, the user's full question.
- Inverted index
- A table from each word to the list of documents that contain it.
- Posting list
- The list of documents for one word in the index.
- Ranking
- Ordering documents by a score, best first.
- Relevance
- Whether a document helps with the need. Decided by a person, not by the system.
- Qrels
- The file of relevance judgments: for each topic, which documents were judged, and how relevant.
- Run file
- A system's output: for each topic, a ranked list of document ids.
Core The idea
Step 1: build an index before anyone asks
You cannot read a hundred million documents each time a question comes in. So a search engine reads them once, in advance, and builds an index. The most common kind is the same as the index at the back of a textbook: for each word, where it appears.
Take three tiny documents:
| Id | Text |
|---|---|
| D1 | the council approved the bridge repair |
| D2 | the bridge lane was postponed |
| D3 | the library extended its hours |
The inverted index turns this around. It goes from words to documents:
| Word | Posting list |
|---|---|
| bridge | D1, D2 |
| council | D1 |
| library | D3 |
| repair | D1 |
| the | D1, D2, D3 |
For the query “bridge repair”, the engine looks up two posting lists and never touches D3. That is why search is fast.
Step 2: score and rank
D1 and D2 both contain “bridge”. Which is better? The engine gives each candidate a score. A good score follows two simple ideas:
- Rare words matter more. “repair” is in one document, “the” is in all three. A match on “repair” says more.
- More matches are better, up to a point. A document that mentions “bridge” five times is probably more about bridges than one that mentions it once, but not five times more.
D1 matches both query words, so it ranks first. The standard formula built on these two ideas is BM25, the subject of Chapter 5.
Two families of search
Matches the words themselves, through an inverted index. Exact, fast, easy to explain. Misses synonyms: “fix” does not match “repair”. Chapter 5.
A model turns text into a list of numbers that captures meaning. Similar meanings land close together. Finds synonyms, can miss exact names and numbers. Chapter 6.
Step 3: how researchers test a search system
To compare two systems fairly you need a test collection. It has three parts:
- Documents: a fixed corpus that every system searches.
- Topics: a fixed set of questions.
- Relevance judgments: for each topic, people have marked which documents are relevant. The file is called the qrels.
Each system produces a run file. In TREC format, every line names one document for one topic:
rag2026-0 Q0 doc_48213 1 0.04865 my-run-tag
rag2026-0 Q0 doc_10977 2 0.04789 my-run-tag
rag2026-0 Q0 doc_70301 3 0.03252 my-run-tag
The six columns are: the topic id, a fixed “Q0”, the document id, the rank, the score, and the name of the run. A scoring program compares the run file with the qrels and prints numbers such as precision and recall (Chapter 4). The document ids in this example are invented.
Because the documents, topics and judgments are fixed, a system built ten years later can be scored on the same collection. This method is the heart of TREC (Chapter 23).
Watch and read
- VideoInverted Index & TF-IDF Explained Step by Step LatentQuest. Builds an index by hand and scores documents. Link checked 14 Nov 2026
- CodePyserini University of Waterloo. A Python toolkit for reproducible search research, with keyword and embedding search. The TREC RAG 2026 search service is built on it. Read 14 Nov 2026
- SiteTREC RAG 2026, the retrieval task. The run-file format a team must submit. Linked from the track page, 14 Nov 2026
The classic free textbook is “Introduction to Information Retrieval” by Manning, Raghavan and Schütze. Its website did not respond when we checked on 14 November 2026, so the link will be added later.
How we did it at TREC 2026
In the RAG track we never built an index ourselves. The organizers host the ClimbMix collection behind a search service, and a team sends queries to it with an access token and gets ranked document ids and text back. For teaching, this is good news: a student team can take part without a large server.
The test set has 119 topics, called narratives, with ids rag2026-0 to rag2026-118. Our retrieval runs are files in the format shown above, one for each of our eight submitted runs. Each line was written by a short script with the run tag in the last column, for example isu-bm25-rrf7-deep.
One thing is different from the textbook picture. In 2026 the retrieval task asks for “all and only” the relevant documents, so the number of lines per topic is the system's own choice. Chapter 10 is about that choice.
What surprised us
Try it
- On paper, build the inverted index for five sentences of your own. Use it to answer a two-word query without rereading the sentences.
- In Python, build the index as a dictionary from word to a set of document ids. Write a function that returns documents containing all query words, and another for any query word.
- Write three lines of a run file by hand for one topic. Then explain to a classmate what each column means.
Check yourself
- What does an inverted index map from, and to?
- Why does a match on a rare word count for more?
- Give one query that keyword search handles better than embedding search, and one the other way round.
- What are the three parts of a test collection?
- In a TREC run file, which column holds the rank?
Answers
- From each word to the list of documents that contain it.
- It separates documents better. A word that is in every document tells you nothing about which one to pick.
- Keyword: an exact product code or a person's name. Embedding: a question phrased in words that the document does not use.
- Documents, topics and relevance judgments.
- The fourth.
Go deeper
- Text processing changes results. Lower-casing, removing very common words and cutting words to their stems all happen before indexing. Two engines with the same formula can disagree because of these steps alone.
- First-stage and later stages. Modern systems are cascades: a fast first stage finds a few hundred candidates from millions, and slower, better models re-order them (Chapter 9). The first stage sets the ceiling.
- Judgments are incomplete. Nobody judges every document for every topic. An unjudged document is usually counted as not relevant, which can punish a new system that finds new things.
- Project idea. Index 10,000 documents with Pyserini. Run the same 20 queries with and without stemming, and count how many of the top ten change.