liveBook · Manning

Embedding-based retrieval

As explained in Build an Advanced RAG Application (From Scratch)

A bi-encoder independently maps a query and document to vector representations for efficient similarity comparison.

Embedding-based retrieval maps a query and each document or review into vector representations, then ranks candidates by vector similarity. A bi-encoder encodes the two inputs independently, unlike a cross-encoder that processes them together. section 4.3 section 4.6

The practical workflow is straightforward: encode the document collection once, store the embeddings, encode each incoming query, calculate cosine similarity or Euclidean distance, and return the highest-ranked results. Precomputation makes retrieval fast and reduces repeated computation. section 4.0 section 4.6

A minimal retrieval implementation follows these steps:

  • Encode hotel reviews with a Sentence Transformers model and retain the resulting vectors. section 4.0
  • Encode the query, compute dot products and vector norms, divide to obtain cosine similarities, and select the top k indices. section 4.3
  • Scale storage and search with FAISS, choosing the model and distance metric according to dataset size and application requirements. section 4.6

Hotel search query

For “Hotel near the Louvre with great food nearby,” encode the query, compare it with review embeddings, and print the top five hotel names, reviews, and similarity scores. The returned results can surface relevant hotels even when their reviews do not repeat the query wording. section 4.3 section 4.0

Figure 4.4 An overview of the Bi-Encoder Model to measure the similarity between two sentences.

Use the bi-encoder architecture figure to trace how the query and comparison sentence become separate representations for similarity measurement. section 4.3

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Explains independent encoding, similarity calculation, ranking, and FAISS scaling, but stops short of detailed model internals.
Examples
Uses Paris hotel reviews, a Louvre query, NumPy cosine search, ranked outputs, and the Travelle search engine.
What it assumes
Builds from semantic search, embeddings, and distance metrics while introducing encoder architectures through the retrieval workflow.
What you can do after
Apply it via Sentence Transformers and FAISS to encode documents, rank query matches, and retrieve relevant results efficiently.
How it connects
Connects semantic search with embeddings, cross-encoders, cosine similarity, Euclidean distance, Sentence Transformers, and FAISS.

Also explained in

Compare how 4 books explain embedding-based retrieval →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.