Lesson 2 of 5 Performance Optimization for GenAI

Optimizing Retrieval Speed and Relevance

This lesson covers index optimisation, query preprocessing and hybrid search with custom scoring, since in RAG applications retrieval latency adds directly to time to first token and its relevance decides answer quality.

Learning outcomes

After completing this lesson, you will be able to:

  • Tune vector index settings for speed and recall.
  • Preprocess and filter queries.
  • Tune hybrid search weights on labelled queries.

Index optimisation

Vector index settings determine both search speed and recall, the share of true nearest neighbours that a search finds. Most vector stores use the HNSW (Hierarchical Navigable Small World) algorithm, a graph that is searched approximately rather than exhaustively. Its parameter m sets how many connections each vector has, and ef_search sets how many candidates a search examines; lower values are faster but find fewer true neighbours.

Index size also matters, because an index that fits in memory is searched far faster than one that must be read from disk. Quantization, which stores vectors at lower precision, and lower embedding dimensions shrink the index. Correctly sized shards and replicas, removal of deleted and duplicate documents and periodic merging of index segments keep searches efficient. Every change is validated with a retrieval test set, so that a gain in speed does not come with an unnoticed fall in recall.

Sort into groups

Speed or recall?

Exam skills 4.2.2 · 1.4.3

Decide whether each HNSW or index change mainly improves speed or recall.

Log in to try this activity Interactive activities are free with an account.

Query preprocessing and filtering

Work done on the query before searching improves both speed and relevance. Query preprocessing normalises text, corrects spelling, expands abbreviations, extracts filters such as a product, date or region mentioned in the question, and rewrites conversational follow-ups into standalone queries.

Metadata pre-filtering then restricts the search to a subset of the index, such as documents about one product line, which reduces the work the search must do and removes irrelevant results before they can distract the model. Simple routing can skip retrieval altogether for greetings or general questions that need no company knowledge. Caching the embeddings of frequent queries saves an embedding call on every repeat of those queries.

Scenario questions

Faster, cleaner retrieval

Exam skills 4.2.2 · 1.5.5

Choose the retrieval optimisation for each problem.

Log in to try this activity Interactive activities are free with an account.

Hybrid search with custom scoring

Hybrid search combines keyword and vector results; custom scoring tunes how. Weights between lexical and semantic scores are tuned on labelled queries, and can differ by query type, for example favouring lexical matches when the query contains an identifier. Additional scoring functions boost recent documents, authoritative sources or documents in the user's language. A reranker on a moderate candidate set adds precision; retrieving fewer final chunks reduces generation latency and cost. The following table summarises retrieval optimisations.

OptimizationImprovesValidate with
HNSW and quantisation tuningLatency and memoryRecall at k on a test set
Metadata pre-filteringLatency and precisionFilter correctness
Query rewriting and normalisationRelevanceRetrieval test set with conversational queries
Hybrid weights and boostsRelevance for mixed query typesMRR and recall by query type
Fewer final chunks after rerankingGeneration latency and costAnswer quality evaluation

The following diagram compares slow, noisy retrieval before and after changes to filtering and index settings.

Fixing slow, noisy retrieval. Searching every product line took 900 ms and returned irrelevant chunks. Passing the product the user has open as a metadata filter, tuning ef_search on a test set and keeping the top five reranked chunks cut latency and prompt size and remove irrelevant context.
Each change is measured on the same test set.
Worked example

Slow, noisy retrieval

Retrieval takes 900 ms and returns chunks from all product lines, although users always ask about the product they have open.

Passing the open product as a metadata filter narrows the search, cutting latency and removing irrelevant chunks; tuning ef_search on a test set trims latency further without recall loss; and keeping the top 5 reranked chunks reduces prompt size.

Check yourself

Against what should HNSW and quantisation settings be tuned?

Show answer

A recall test set, so latency gains do not silently reduce retrieval quality.

Exam Tips

Remember for the exam

  • Tune HNSW and quantisation against a recall test set.
  • Pre-filter on metadata to cut latency and noise.
  • Rewrite and normalise queries before searching.
  • Tune hybrid weights and boosts per query type.