Overview

4 Embeddings and Vector Search

Embeddings are the foundation of reliable retrieval-augmented generation systems because they convert text into vectors that can be searched by meaning rather than exact keywords. The chapter explains that when user queries and documents are embedded into the same vector space, relevant content should appear nearby, enabling the system to retrieve useful context for an LLM. If the embedding model poorly represents the domain or fails to capture important nuances, the system may retrieve irrelevant documents, miss correct answers, or cause the LLM to respond vaguely or incorrectly. Choosing an embedding model therefore requires considering domain fit, privacy, cost, latency, dimensionality, context length, multilingual needs, and hardware constraints.

The chapter compares commercial, open-source, instruction-tuned, and domain-specific embedding models, emphasizing that there is no universal best choice. Commercial APIs are easy to adopt and strong for general use, while open-source models provide transparency, local deployment, and fine-tuning flexibility. Domain-specific models can significantly improve retrieval in fields such as healthcare, law, finance, and technical documentation. The chapter also shows why dense vector search alone is often insufficient: it may capture broad semantic similarity but miss exact terms, region-specific details, or the most answer-bearing passage. To address this, production systems often use hybrid retrieval, combining dense embeddings with sparse keyword methods such as BM25, then applying multi-stage reranking with cross-encoders to improve precision.

The chapter also covers production techniques needed to scale and maintain retrieval systems. Metadata filtering helps restrict results by attributes such as date, region, department, authority, or status, while careful chunking ensures documents are split at useful boundaries without losing context. Vector databases and indexing methods such as HNSW and FAISS make large-scale similarity search practical, and managed or open-source vector stores add persistence, filtering, updates, replication, and observability. Finally, the chapter highlights long-term operational concerns: embedding drift can occur as language, documents, or model versions change, so embeddings must be refreshed consistently; vector compression can reduce memory and cost while preserving enough semantic quality for first-stage retrieval. Overall, the chapter frames embeddings not as background plumbing, but as the meaning map that determines whether a RAG system can find the right evidence and produce reliable answers.

Document embedding in RAG systems
How embedding quality affects RAG retrieval
Visualizing embeddings across a vector space. Each point represents a document that has been mapped to a specific location based on its semantic content
Airbnb’s dual-encoder architecture explained with a sample user’s query
Multi-stage retrieval process
The document chunking dilemma illustrated with a policy document example. Small chunks (top) may separate related information, causing retrieval to miss crucial context when answering specific queries. Large chunks (bottom) keep related information together but may have diluted relevance scores due to inclusion of unrelated content. This fundamental trade-off significantly impacts your RAG system's ability to find and retrieve appropriate information.
Hierarchical Navigable Small World (HNSW) indexes vectors in multiple layers. The search path demonstrates how queries efficiently traverse from sparse upper layers to dense lower layers.

Summary

  • Embeddings are dense vector representations of text that allow systems to retrieve information based on meaning rather than exact wording. They power semantic retrieval in many RAG systems, though RAG can also use keyword-based or hybrid retrieval approaches.
  • In a RAG setup, the embedding model determines which documents are retrieved for the language model to use. If embeddings fail to capture domain nuances, the LLM is forced to answer with poor or irrelevant context.
  • Embedding quality directly influences retrieval accuracy. Choosing a model that matches your domain and query style, whether from commercial APIs, open-source libraries, or domain-specific sources, is critical to system reliability.
  • Commercial embedding APIs (like OpenAI and Cohere) offer high performance with low setup cost but limit customization. Open-source models (like E5 and BGE) offer more control and privacy, especially for specialized use cases.
  • Hybrid and multi-stage retrieval pipelines combine dense semantic search with sparse keyword-based methods like BM25, and improve ranking with cross-encoders to boost both recall and precision. Cross-encoder reranking is valuable in any RAG pipeline, not just hybrid ones, as it directly scores query-document relevance more accurately than bi-encoder similarity alone.
  • Chunking strategy has a major impact on retrieval success. Small chunks risk losing context, while overly large ones dilute relevance. Overlapping, content-aware chunking provides a practical balance.
  • Metadata filtering adds structure-aware retrieval capabilities by filtering results based on attributes like geography, policy status, or document type, essential for trusted systems in enterprise settings. For best results, label every chunk with metadata such as section headings, page numbers, chapter titles, file names, and publication dates. Without this structured context, RAG can fail on even simple queries over large documents.
  • Efficient search at scale relies on approximate nearest neighbor algorithms like HNSW, implemented in libraries like FAISS and deployed in vector databases such as Pinecone, Weaviate, and Qdrant.
  • Two common production issues are embedding drift (when embeddings become outdated due to content or model changes) and storage bloat from high-dimensional vectors. Drift is most significant when your content changes frequently or when you switch embedding models (which requires re-embedding your entire corpus). Choosing a sufficiently general embedding model can reduce the frequency and cost of re-embedding. Compression techniques like product quantization keep systems fast and scalable as your index grows.

FAQ

What are embeddings in a RAG system?

Embeddings are numerical vector representations of text, such as sentences, paragraphs, or documents. In a RAG system, both documents and user queries are converted into vectors in the same high-dimensional space. The system retrieves documents whose vectors are closest to the query vector, allowing it to search by meaning rather than exact keyword matches.

Why does embedding quality matter so much for reliable AI answers?

Embedding quality determines whether the retrieval system finds the right context for the LLM. If relevant documents are placed near the user query in vector space, the LLM can generate grounded and accurate answers. If the embedding model misunderstands the domain or maps related content far apart, the system may retrieve irrelevant passages, miss the answer entirely, or cause the LLM to produce vague, incorrect, or hallucinated responses.

How is semantic search different from keyword search?

Keyword search looks for exact or near-exact term matches, while semantic search uses embeddings to find text with similar meaning. For example, a good embedding model can connect “How do I get reimbursed for therapy?” with “What’s the process for submitting a mental health claim?” even though the wording differs. Semantic similarity is commonly measured with cosine similarity or dot product.

How should I choose between commercial, open-source, and domain-specific embedding models?

There is no universal best embedding model. Commercial APIs such as OpenAI, Cohere, and Google are easy to use and strong for general-purpose search, but they can introduce cost, privacy, latency, and vendor-lock-in concerns. Open-source models such as E5, BGE, all-MiniLM, Qwen3-Embedding, and NV-Embed-v2 offer more control, local deployment, and fine-tuning options, but require infrastructure. Domain-specific models such as LegalBERT, PubMedBERT, BioSentVec, and CodeBERT can outperform general models in specialized areas like law, medicine, and code, but may generalize poorly outside their domain.

What are dense retrieval and sparse retrieval?

Dense retrieval uses embeddings to capture semantic meaning and retrieve conceptually related documents. Sparse retrieval uses keyword-based methods such as BM25 or TF-IDF to find documents containing important exact terms. Dense retrieval is good at matching related ideas like “quitting” and “resignation,” while sparse retrieval is strong for precise terms like “California,” “30-day notice,” or policy IDs.

Why use hybrid retrieval instead of pure vector search?

Pure vector search can miss relevant information because of semantic drift, top-k recall limits, or domain gaps. Hybrid retrieval combines dense vector search with sparse keyword search, giving the system both semantic understanding and exact-term precision. A typical hybrid pipeline runs BM25, runs vector similarity search, merges and deduplicates the results, and optionally reranks them with a more precise model.

What is multi-stage retrieval, and why is reranking useful?

Multi-stage retrieval separates retrieval into layers. First, a broad retrieval stage finds a large set of potentially relevant documents. Then filtering narrows the candidates. Finally, a reranker, often a cross-encoder, scores query-document pairs more precisely. This improves precision by ensuring the most relevant documents rise to the top, even if the initial hybrid retriever found them but ranked them too low.

What is the difference between a bi-encoder and a cross-encoder?

A bi-encoder embeds the query and each document separately, then compares their vectors using a similarity metric such as cosine similarity. This is efficient because document embeddings can be precomputed. A cross-encoder takes the query and document together as a single input and outputs a relevance score. Cross-encoders are slower but more accurate because the query and document directly interact inside the model, making them useful for reranking a small candidate set.

Why are metadata filtering and chunking important in production RAG systems?

Metadata filtering helps ensure retrieved documents are not only semantically relevant but also applicable. Filters can account for attributes such as status, region, department, effective date, audience, and document authority. Chunking determines how documents are split before embedding. Chunks that are too small may lose context, while chunks that are too large may dilute relevance. Effective systems often use content-aware chunking, such as section-based chunking for policies, paragraph-based chunking for narratives, and overlap to avoid losing information at boundaries.

What role do vector databases, HNSW, and FAISS play in scaling retrieval?

As document collections grow, naive vector search becomes too slow because it compares the query against every vector. HNSW, or Hierarchical Navigable Small World, speeds up approximate nearest neighbor search by organizing vectors into a navigable multi-layer graph. FAISS provides optimized vector indexing implementations, including HNSW, IVF, and product quantization. Vector databases such as Pinecone, Weaviate, Qdrant, Chroma, pgvector, Milvus, and Elasticsearch add production features like persistence, metadata filtering, incremental updates, replication, scaling, and observability.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free