ContentsRAG from Scratch to TRECAdisak Sukul · Iowa State University

Start here

Part 1 · Foundations

Part 2 · Finding the evidence

Part 3 · Writing the answer

Part 4 · Judging RAG

Part 5 · TREC and the 2026 tracks

Part 6 · Case studies: our submissions

Part 7 · Beyond text: video

Part 8 · Streams and advanced RAG

Back matter

Part 2 · Finding the evidence · Chapter 6

Dense retrieval and embeddings

In one sentence: an embedding model turns a text into a list of numbers so that texts with similar meaning get similar numbers, and dense retrieval finds the documents whose numbers are closest to the query's.

Why you should care

Keyword search fails when the question and the answer use different words. Embeddings match on meaning, so “car” can find “automobile”, and with the right model an English question can find a Chinese document. Most RAG tutorials begin here. You should know both what this buys and what it costs.

Words you need

Vector
A list of numbers. Here, usually several hundred to a few thousand of them.
Embedding
The vector a model produces for a text.
Embedding model
A neural network trained so that related texts get vectors that point the same way.
Cosine similarity
A number from −1 to 1 that says how closely two vectors point in the same direction.
Dense retrieval
Search by nearest vectors. “Dense” because almost every number in the vector is non-zero.
Bi-encoder
A model that embeds the query and the document separately. Document vectors can be computed once, ahead of time.
Vector index
A data structure that finds the nearest vectors quickly without comparing against all of them.
Hybrid search
Keyword search and dense search used together.

Core The idea

Texts as points

Think of each text as a point in space. An embedding model is trained on a very large number of text pairs that belong together, such as a question and its answer. Training pulls such pairs close and pushes unrelated texts apart. After training, closeness in the space stands for closeness in meaning.

Search then has two phases:

  1. Ahead of time: embed every document and store the vectors in a vector index.
  2. At question time: embed the query with the same model and return the documents whose vectors are closest.

Measuring closeness

The usual measure is cosine similarity. Multiply the two vectors number by number, add up, and divide by the two lengths.

cosine(a, b) = (a · b) / (|a| × |b|)

A toy example with three numbers per vector. Real vectors are much longer, and the arithmetic is the same.

TextVectorCosine with the query
Query: “Was the crossing fixed?”(0.9, 0.1, 0.3)
Document 1: “Crews finished repairs on the bridge.”(0.8, 0.2, 0.4)0.86 / (0.954 × 0.917) = 0.984
Document 2: “The bakery opens at six.”(0.1, 0.9, 0.2)0.24 / (0.954 × 0.927) = 0.271

Document 1 shares no content word with the query, and it still scores close to 1. BM25 would give it zero.

What it costs

BM25Dense retrieval
MatchesWordsMeaning
Exact names, codes, numbersStrongOften weak
Different wording or languageWeakStrong
SetupBuild an inverted index. Minutes to hours on a processor.Run a neural network over every document. Usually needs a graphics card.
Can you explain a score?Yes, term by termHardly
New kinds of textWorks the same everywhereCan drop on text unlike its training data

The two fail in different places. That is the reason for hybrid search: run both and fuse the results (Chapter 8).

Watch and read

For the two videos we confirmed that the link opens and the title and channel match. We have not watched them through.

How we did it at TREC 2026

We used embeddings in two tracks, and not in the third. All three choices have a reason.

TrackFirst-stage searchWhy
RAGBM25 only, through the track's search serviceWe did not hold the collection. We searched it through the hosted service and put our effort into queries, fusion and reranking.
RAGTIMEDense only, through the track's search service, which uses Qwen3 embeddingsThe documents are in four languages. Meaning-based search crosses languages; word matching does not (Chapter 11).
Change DetectionOur own BM25 in five runs. Hybrid, BM25 plus bge-m3 embeddings, in two runs.We held the news collection, so we could index it and embed it ourselves on the university's GPU cluster.

In the two hybrid Change Detection runs, fx40 and fburst, each document gets two scores for each question: a BM25 score scaled to the range 0 to 1, and the cosine between the bge-m3 vectors of the question and the document. The final score is half of one plus half of the other.

hybrid score = 0.5 × BM25 (scaled) + 0.5 × cosine
A document found by only one of the two searches gets a low stand-in value for the other: 0.02 for BM25, 0.30 for cosine. So it is damped, not dropped.

Why 0.30 for the missing cosine and not zero? Cosine scores from an embedding model seldom come near zero, even for unrelated texts. A zero would punish a document far more than “not in the dense top list” deserves. This is a small detail that matters whenever you add scores from two different systems.

What surprised us

Adding embeddings helped, but we cannot say by how much. In Change Detection our hybrid run fx40 scored higher than our best BM25 run exp35 on the track's ranked measure (NDCG′): 0.5895 against 0.4792, and higher on all 31 topics. But the two runs also use different decision thresholds, 0.40 and 0.35. Two things changed at once, so the gain cannot be assigned to the embeddings alone. We also never submitted a run with embeddings only. And in the RAG track we never ran a dense first stage, so we cannot say whether it would have helped there. Our choices show what we could build with the access we had. They do not prove those choices were the best ones.

Try it

  1. Compute the cosine of (1, 2, 0) and (2, 1, 1) by hand. You should get 0.730.
  2. With the sentence-transformers package and a small model, embed the twenty sentences from the Chapter 5 lab. Search them with the query you saved, the one that shares no word with its answer. Does dense search find it?
  3. Now find a query where BM25 wins. A product code or a rare family name is a good place to look.
  4. Print the cosine between two unrelated sentences. How far from zero is it?

Check yourself

  1. What does “dense” mean in dense retrieval?
  2. Why can document vectors be computed before any question arrives?
  3. Give one query type where dense search is likely to beat BM25, and one where it is likely to lose.
  4. In our hybrid formula, why does a missing cosine score become 0.30 and not 0?
  5. Why did we use BM25 only in the RAG track?
Answers
  1. Almost every number in the vector is non-zero, unlike word-count vectors, which are mostly zeros.
  2. A bi-encoder embeds the document without looking at the query.
  3. Wins: a question worded differently from the answer, or in another language. Loses: an exact name, code or number.
  4. Cosine scores of unrelated texts are usually well above zero, so zero would be an unfairly heavy penalty.
  5. The collection was reached through a hosted search service. We used its BM25 search and spent our effort on later steps.

Go deeper

  • How the models are trained. Contrastive learning: for each query, one matching passage and many non-matching ones. The hard part is choosing non-matching passages that are difficult but truly wrong, called hard negatives.
  • Finding neighbours fast. Comparing a query with every vector is too slow at scale. Approximate nearest-neighbour indexes such as HNSW and IVF trade a little accuracy for a large gain in speed. Quantization shrinks each vector to save memory.
  • One vector or many. ColBERT keeps one vector per token and compares them all (“late interaction”). It is more accurate and needs far more storage. bge-m3 can produce dense, sparse and multi-vector outputs from one model.
  • Long documents. A single vector for a long document blurs its topics. This is one reason for chunking (Chapter 40).
  • Out-of-domain drop. The BEIR benchmark showed that dense models trained on one kind of data can fall below BM25 on another. Test on your own data before you trust a leaderboard.
  • Project idea. On one public collection, run BM25, one dense model and their RRF fusion. Report on which topics each one wins, not only the averages.