Retrieval decides which evidence reaches the prompt. Missing evidence caps what a grounded answer can claim, even though prompts and generation models can still improve how fixed evidence is used. The practical problem is to recover enough relevant material without filling the context window with noise or spending the entire latency budget on search.

A query can become a vector for semantic search, weighted terms for keyword search, or both. Vector search compares it with pre-indexed chunk embeddings. BM25 searches a lexical index. The resulting candidates are fused into one ranked list and passed to reranking or directly to the generator.

But no single search path handles every query shape well.

flowchart LR
    Q[User query] --> D[Vector search]
    Q --> S[Keyword search]
    D --> F[Fuse results]
    S --> F
    F --> R[Top-k candidates]
    R --> N[Reranker or generator]

An example is the query “rate limit error 429 behavior in partner tier.” Vector search recognizes the meaning around rate limiting, yet may lose the exact token 429. Keyword search catches 429 and partner tier, while missing related passages that use different wording. Running both paths covers those two failure modes. Hybrid search is a sensible default, though the added path can introduce noise on some corpora.

Retrieval Modes

The index-time and query-time work split cleanly:

  • An embedding model maps each document chunk and the query into the same fixed-size vector space. Document vectors are computed once during indexing. Each request only needs a query vector.
  • The vector database searches an approximate index, commonly HNSW or IVF, for nearby chunk vectors. Approximation gives up some recall to avoid scanning millions of vectors one by one.
  • Model choice shapes the result set. Dimensionality and training data differ, and a model that leads a general MTEB benchmark may fail on a specialized corpus or another language. Domain queries are the real test.

Dense retrieval fits:

  • Semantic paraphrases and natural-language questions where user wording differs from source text.
  • Multilingual corpora where the embedding model captures meaning across languages.

The main risk is specific:

  • Misses exact identifiers. Embedding models capture meaning better than literal tokens. Error codes, API paths, and version strings can disappear inside the vector representation. Returned chunks may look topically correct while describing the wrong operation, which is harder to catch than an obviously unrelated keyword result.

Sparse Retrieval — Keyword Search (BM25)

BM25 is the default ranking model in search engines such as Elasticsearch. PostgreSQL’s native full-text search uses to_tsvector/to_tsquery for lexeme matching and ts_rank or ts_rank_cd for ranking. It does not provide BM25 as its built-in ranker. Both behave like ranked grep at a high level, but their scoring functions differ. A search for NullReferenceException in a .NET codebase hits files containing that string. A focused source file can rank above a 10,000-line log that mentions it once.

How it works:

  • BM25 gives rare matching terms more weight. If only three documents contain E4392, they score strongly for that query. Common words contribute little.
  • Repetition has diminishing returns, and a length penalty stops large documents from winning through word count alone.
  • A standard keyword index is enough. No embedding model, GPU, or vector database is involved.

Where it fits:

  • Queries with exact keyword constraints: error codes, product names, version numbers, configuration keys.
  • Domains with specialized terminology where exact matches carry high signal.

Main risk:

  • Weak on paraphrases. BM25 cannot connect “authentication failure” with “credential validation errors” when the words do not overlap. Stemming and synonym expansion help at the edges, but semantic variation remains its blind spot.

Hybrid Retrieval — Vector + Keyword

Hybrid retrieval runs full-text search, such as WHERE body @@ to_tsquery('error & 429'), beside vector similarity search and then merges the results. RRF works from rank positions. If document A is #2 in vector search and #5 in keyword search, while document B is #1 in keyword search but #200 in vector search, the agreement around A raises it in the fused list. Linear combination instead assigns an explicit weight to each score, closer to a weighted UNION ALL.

How it works:

  • Vector search and keyword search run in parallel against the same query, and the two ranked lists are fused into a single candidate set.
  • Reciprocal Rank Fusion (RRF) sums 1 / (k + rank) for each document across the input lists, with k=60 in the original method. Because it uses positions instead of scores, BM25 and vector similarity do not need a shared scale. Agreement across retrievers is rewarded.
  • Linear combination normalizes scores and computes alpha * vector_score + (1 - alpha) * keyword_score. It exposes more control, but alpha must be tuned for the domain. Identifier-heavy corpora may need more keyword weight. Conversational queries often favor the vector path.

Where it fits:

  • Production systems with mixed query patterns. This is a safe default for many RAG systems.

Main risk:

  • Can lose to one search mode. A homogeneous corpus may strongly favor one retriever, leaving the other to inject noise. In one published case study of 156 multilingual engineering and scientific queries, vector-only reached a 69.2% hit rate while hybrid reached 63.5%. The extra keyword path hurt. Hybrid must be tested against both single-mode baselines on the actual corpus.
  • Large top-k values collect noise. Fusing two long result lists pulls weak candidates into the tail. Without reranking or deduplication, those candidates dilute the context sent to generation.

Indexing and Filtering

The vector index sets the latency-recall curve:

  • HNSW builds a layered graph between nearby vectors and searches from coarse connections down to local neighbors. Raising ef_search explores more of that graph, buying recall with latency. A value that works on a small collection may miss more neighbors after the corpus grows.
  • IVF-PQ clusters and compresses vectors, in some configurations using far less memory than HNSW. Aggressive compression loses recall more quickly. It suits collections around 10M+ vectors when an in-memory HNSW graph is too expensive.

Filtering decides which vectors are eligible:

  • Pre-filtering narrows the search space by metadata before vector search, so the returned candidates already satisfy the filter. Choosing it over post-filtering is a recall and cost decision. Authorization is a separate requirement: semantic similarity cannot enforce it, so tenant and ACL boundaries hold only when unauthorized chunks are excluded before any result reaches the model or the user, whichever stage performs the exclusion.
  • Post-filtering removes candidates after ANN search. It is easier to bolt on, but selective filters may discard most of top-k and leave too little evidence.
  • Index versions stay explicit, and collection aliases enable instant rollback during index rebuilds.

Pitfalls

ANN Operating-Point Regression

Corpus growth or distribution change can make an earlier HNSW ef_search setting miss more exact neighbors. The outcome depends on graph construction, filters, data distribution, implementation, and workload; latency can also move. Nothing in ordinary error-rate monitoring establishes that recall stayed stable.

Detection: scheduled Recall@k checks compare ANN results with exact search on a maintained set of representative queries. Whether the response is tuning ef_search, rebuilding, or changing index parameters follows from the measured recall-versus-latency curve; no single response is assumed to restore the old operating point.

Embedding Model Migration Debt

Embedding models produce incompatible vector spaces. An upgrade therefore requires re-embedding the corpus. New query vectors cannot be compared with an old index. At scale, the migration needs parallel storage and a controlled cutover, and benchmark gains do not guarantee better domain retrieval. Provider deprecations can compress that schedule.

Mitigation: embedding model selection is a long-term infrastructure decision. Each vector is stored with its model version, upgrade thresholds come from domain-specific metrics rather than MTEB deltas, and collection aliases with shadow traffic allow validation before cutover.

Aggregate Metrics Hiding Segment Failures

An overall recall of 70% can coexist with 5% recall on a small but valuable query segment. Multi-hop or date-filtered requests may be failing while the aggregate looks acceptable. Segmentation also separates missing inventory from a retriever that cannot surface material already present.

Detection: retrieval metrics are segmented by query type, tenant, locale, and domain, and alerts fire on per-segment degradation, not just on the aggregate.

Vector Search Failing Silently on Identifiers

For identifier-heavy queries, vector search can return chunks that are topically related and operationally wrong. The evidence looks plausible enough for the model to assemble a confident answer from the wrong material.

Mitigation: identifier-heavy corpora are a strong case for hybrid retrieval, and their evaluation includes identifier queries. If the vector path causes most failures, the fusion weights are the first place to look, because these domains often need more keyword influence.

Tradeoffs

ModeRecall profileLatencyOperational complexityBest for
Vector onlyStrong on semantic paraphrases — weak on exact identifiersLow — single index lookupModerate — embedding model and vector database requiredHomogeneous semantic corpora with natural-language queries
Keyword only — BM25Strong on exact terms — weak on paraphrasesLowest — keyword index lookupLow — no embedding model or vector databaseIdentifier-heavy domains with stable vocabulary
Hybrid — RRFBroad — covers semantic and lexical queriesModerate — two parallel searches plus fusionHigher — two indexes and fusion logicMixed query patterns — default for most production systems
Hybrid — linear combinationTunable — weight toward dominant search modeModerate — same as RRFHighest — requires alpha tuning per domainWhen one search mode is consistently stronger and deserves explicit weighting

Hybrid retrieval with RRF and a conservative top-k, often 5–20, is a reasonable first configuration. It is then compared with both single-mode baselines on real corpus queries, because hybrid is a good starting point, not a guaranteed winner. Reranking fits after first-stage retrieval is stable and the remaining failures come from poor ordering near the top.

Questions

References