Inferensys

Glossary

EF Search (HNSW)

EF Search is a critical hyperparameter in the Hierarchical Navigable Small World (HNSW) algorithm that controls the size of the dynamic candidate list during graph traversal, directly trading off between higher search accuracy (recall) and increased query latency.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
VECTOR QUERY OPTIMIZATION

What is EF Search (HNSW)?

EF Search is a critical hyperparameter in the Hierarchical Navigable Small World (HNSW) algorithm that directly controls the trade-off between search accuracy and query speed.

EF Search (short for Entry Factor during Search) is a runtime parameter that defines the size of the dynamic candidate list maintained during the graph traversal phase of an HNSW query. A larger EF Search value expands the search frontier, evaluating more potential neighbor nodes, which increases the probability of finding the true nearest neighbors (recall) but also increases query latency and CPU cost. This parameter is distinct from the construction-time efConstruction parameter.

Tuning EF Search is a primary method for managing the accuracy-latency trade-off in production. For high-recall scenarios like recommendation systems, a higher value (e.g., 200-500) is used. For low-latency applications like real-time retrieval, a lower value (e.g., 10-100) is sufficient. It operates in conjunction with the M parameter, which controls the graph's connectivity, and is central to the beam search heuristic that powers HNSW's efficiency.

HNSW HYPERPARAMETER

Key Characteristics of EF Search

EF Search is the primary runtime parameter controlling the trade-off between search accuracy (recall) and query latency in the Hierarchical Navigable Small World (HNSW) algorithm.

01

Dynamic Candidate List Size

EF Search defines the size of the dynamic list (ef) maintained during graph traversal. At each layer of the HNSW graph, the algorithm keeps a priority queue (the 'dynamic candidate list') of the ef most promising neighbor nodes to explore next. A larger ef value allows the search to consider more candidates, increasing the probability of finding the true nearest neighbors but at the cost of more distance computations and longer latency.

  • Core Mechanism: Governs the beam width of the search.
  • Default Values: Often set between 10 and 200, depending on the required recall-latency profile.
02

Recall vs. Latency Trade-off

This parameter directly mediates the fundamental performance trade-off in approximate nearest neighbor (ANN) search.

  • High EF Search (e.g., 200-500): Maximizes recall by exploring a wider neighborhood. This is critical for applications requiring high accuracy, such as legal document retrieval or diagnostic image matching, where missing a relevant result is costly.
  • Low EF Search (e.g., 10-50): Minimizes query latency and CPU usage by performing a more greedy, narrow search. Ideal for real-time applications like recommendation systems or chat response retrieval, where sub-50ms latency is required.

Tuning involves running benchmarks to find the optimal point on the recall-latency curve for a specific dataset and performance SLA.

03

Runtime vs. Build-Time Parameter

A critical distinction is that EF Search is a query-time parameter, unlike the M parameter which is an index construction parameter. This means:

  • Dynamic Adjustment: ef can be changed on a per-query basis without rebuilding the index. An application can use a low ef for fast, exploratory searches and a high ef for final, precise retrieval.
  • Independent of Index: The HNSW graph structure, determined by M and efConstruction during build, remains fixed. ef only controls how thoroughly that pre-built graph is traversed.
  • System Flexibility: Allows for implementing multi-stage search pipelines, where a first-pass low-ef search retrieves candidates for a second-pass, high-ef re-ranking.
04

Interaction with Graph Quality (M)

The effectiveness of ef is intrinsically linked to the quality of the underlying HNSW graph, which is built using the M parameter.

  • Well-Connected Graph (Higher M): With more edges per node, even a low ef value can navigate the graph efficiently, as each step offers many high-quality paths. The recall gains from increasing ef may saturate quickly.
  • Sparse Graph (Lower M): The graph has fewer connections. A low ef search is more likely to follow a sub-optimal path into a local minimum. A higher ef is necessary to maintain recall by exploring alternative routes.

Therefore, ef and M must be co-optimized: a higher M often allows the use of a lower ef to achieve the same recall, saving query-time compute.

05

Implementation in Libraries (Faiss, Weaviate)

EF Search is a standardized parameter across major vector database and ANN library implementations.

  • Faiss: The IndexHNSW search method accepts an efSearch argument. For example, index.search(query, k, params=faiss.SearchParametersHNSW(efSearch=128)).
  • Weaviate: Configured at query time via the GraphQL nearVector or nearText filter: nearVector({vector: [...], distance: 0.8, ef: 200}).
  • Milvus/Pinecone: Similar runtime parameters are exposed through their respective SDKs and APIs for HNSW-based indexes.

The consistency of this parameter allows for portable tuning knowledge and performance expectations across different infrastructure platforms.

06

Tuning Methodology & Best Practices

Effective tuning of EF Search follows a data-driven, benchmark-oriented process:

  1. Establish a Baseline: Fix the HNSW index (M, efConstruction) and measure Recall@K and P95/P99 Latency across a range of ef values (e.g., 16, 32, 64, 128, 256) using a held-out query set.
  2. Plot the Trade-off Curve: Create a recall-latency plot. The 'knee' of the curve often represents a good operational point.
  3. Define SLAs: Determine the minimum acceptable recall (e.g., 0.95) and maximum allowable latency (e.g., 20ms) for your application.
  4. Select ef: Choose the smallest ef value that meets your recall SLA without breaching latency limits.
  5. Validate in Production: Monitor real-world performance, as query distribution changes can affect optimal settings.

Best Practice: Implement adaptive EF Search, where the parameter is adjusted dynamically based on query load or result confidence scores.

HNSW PARAMETER

EF Search: Performance Trade-Offs

Comparison of EF Search parameter settings and their impact on query performance, recall, and system resource utilization.

Performance DimensionLow EF Search (e.g., 10-32)Medium EF Search (e.g., 64-128)High EF Search (e.g., 256-512)

Primary Trade-Off

Optimized for low query latency

Balanced latency and recall

Optimized for high recall (accuracy)

Typical Query Latency

< 1 ms

1-5 ms

10-50 ms

Recall@10 (Approximate)

0.85 - 0.95

0.95 - 0.99

0.99 - 1.0

Dynamic Candidate List Size

Small

Medium

Large

Graph Traversal Breadth

Narrow

Moderate

Wide

CPU Utilization per Query

Low

Medium

High

Memory Bandwidth Pressure

Low

Medium

High

Use Case Fit

Real-time applications with strict latency SLOs

General-purpose semantic search

High-accuracy retrieval for evaluation or critical systems

EF SEARCH (HNSW)

Practical Tuning Guidelines

EF Search is the primary hyperparameter for controlling the accuracy-latency trade-off during query execution in an HNSW index. These guidelines detail how to tune it for specific performance profiles.

01

Core Definition & Function

EF Search (short for 'search dynamic candidate list size') is the hyperparameter that defines the size of the dynamic priority queue maintained during the greedy graph traversal in the Hierarchical Navigable Small World (HNSW) algorithm. It directly controls the breadth of the search:

  • A larger ef_search value expands the candidate list, allowing the algorithm to explore more neighbors at each layer. This increases the probability of finding the true nearest neighbors (higher recall) but requires more distance computations, increasing query latency.
  • A smaller ef_search value restricts exploration, making the search faster but more likely to miss optimal vectors, reducing recall. It is purely a query-time parameter and does not affect index build time or memory footprint.
02

Tuning for High Recall (Accuracy-First)

Use this profile for applications where result quality is paramount, such as final-stage retrieval in a Retrieval-Augmented Generation (RAG) pipeline or deduplication tasks.

  • Set ef_search significantly higher than k. A common rule of thumb is ef_search = 10 * k or more. For example, if returning k=10 results, start with ef_search=100.
  • Validate with Recall@K. Measure recall against ground truth results from an exhaustive (flat) search. Incrementally increase ef_search until recall plateaus at an acceptable level (e.g., 0.95-0.99).
  • Monitor Latency Impact. Be aware that latency increases approximately linearly with ef_search. This trade-off is necessary for high-recall scenarios.
03

Tuning for Low Latency (Performance-First)

Use this profile for real-time applications like user-facing search or recommendation systems where speed is critical, and approximate results are acceptable.

  • Set ef_search close to, but slightly above, k. Start with ef_search = k + 10 to k + 50. For k=10, try ef_search=20.
  • Accept Lower Recall. Understand that recall will drop. Use Recall@K to quantify the accuracy loss and ensure it remains within service-level agreement (SLA) bounds.
  • Leverage Multi-Stage Search. In a high-throughput system, consider a two-stage pipeline: a very fast, low-ef_search HNSW search to generate a candidate set, followed by a more precise re-ranking step on a smaller subset.
04

Interaction with M (Construction Parameter)

EF Search and the construction parameter M (maximum connections per node) are deeply interrelated. Tuning one often requires reconsidering the other:

  • A graph built with a higher M is denser and has more local shortcuts. This can allow you to use a lower ef_search to achieve the same recall, as each node offers more high-quality neighbor options to explore.
  • A graph built with a lower M is sparser. To achieve high recall, you will likely need a higher ef_search to compensate for the fewer connections and avoid search stagnation.
  • Tuning Order: First, choose M based on desired index build time and memory constraints. Then, tune ef_search to achieve your target query performance profile.
05

Monitoring & Dynamic Adjustment

EF Search can be adjusted dynamically without rebuilding the index, making it ideal for A/B testing and adaptive performance management.

  • Benchmark with Real Query Logs. Use a sample of production query vectors to measure the recall-latency curve for your specific dataset and index.
  • Set Service-Level Objectives (SLOs). Define target P99 Latency and minimum Recall@K for your service. Tune ef_search to the highest value that still meets your latency SLO.
  • Implement Canary Deployments. When changing ef_search in production, roll out the change gradually while monitoring key metrics: query latency, recall (if measurable), and downstream application performance (e.g., RAG answer quality).
06

Common Anti-Patterns & Pitfalls

Avoid these common mistakes when configuring EF Search:

  • Setting ef_search lower than k. This is invalid and will cause search errors or severely degraded results.
  • Using Extremely High Values Unnecessarily. Setting ef_search to 1000+ for a small dataset (k=10) wastes compute and memory bandwidth with diminishing returns on recall.
  • Ignoring the Data Distribution. The optimal ef_search depends on the 'hardness' of your vector space. Clustered, well-separated data may need a lower ef_search than a uniform, dense distribution.
  • Forgetting to Re-tune After Index Updates. If you significantly expand your dataset and rebuild the HNSW index, the previous optimal ef_search may no longer apply. Re-run your benchmarking process.
EF SEARCH (HNSW)

Frequently Asked Questions

EF Search is a critical hyperparameter in the Hierarchical Navigable Small World (HNSW) algorithm that directly governs the trade-off between search accuracy and query speed. These FAQs address its function, tuning, and impact on production vector search systems.

EF Search (short for ef or efSearch) is the hyperparameter in the HNSW algorithm that controls the size of the dynamic candidate list maintained during graph traversal. During a k-NN search, the algorithm uses a priority queue (often called the "dynamic list" or "candidate set") to explore the graph. The ef value sets the maximum capacity of this queue. A larger ef allows the search to consider more potential neighbor nodes at each layer, leading to a more exhaustive exploration of the graph and a higher probability of finding the true nearest neighbors, but at the cost of increased query latency and CPU usage. The search begins at the top layer of the HNSW graph with an entry point, greedily moving to the nearest neighbor. At each step, it explores the neighbors of the current best node, adding the most promising candidates to the priority queue, which is capped at ef elements. The process repeats until the queue is fully explored, and the top k results are extracted.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.