Overview
Vespa’s vector search implementation (searchlib/src/vespa/searchlib/tensor/hnsw_index.h:29-41):
Implementation of a hierarchical navigable small world graph (HNSW) that is used for approximate K-nearest neighbor search. The implementation supports 1 write thread and multiple search threads without the use of mutexes. This is achieved by using data stores that use generation tracking and associated memory management. The implementation is mainly based on the algorithms described in “Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs” (Yu. A. Malkov, D. A. Yashunin).
Defining Tensor Fields
First, define a tensor field in your schema with HNSW indexing:Distance Metrics
Vespa supports multiple distance metrics:angular (Cosine Distance)
angular (Cosine Distance)
Computes the angular distance between vectors. Best for normalized embeddings.
prenormalized-angular
prenormalized-angular
Optimized for pre-normalized vectors. From the test schema (
config-model/src/test/derived/hnsw_index/test.sd:4-16):euclidean
euclidean
Computes Euclidean (L2) distance between vectors.
geodegrees
geodegrees
For geographic coordinates (latitude, longitude). From test schema (
config-model/src/test/derived/hnsw_index/test.sd:17-23):dotproduct
dotproduct
Computes inner product. Useful for learned embeddings with specific properties.
HNSW Configuration
max-links-per-node
Controls the connectivity of the graph. Higher values improve recall but increase memory usage and indexing time.neighbors-to-explore-at-insert
Number of neighbors to consider during document insertion. Higher values improve index quality but slow down indexing.multi-threaded-indexing
Enable parallel indexing for faster bulk loading:Querying with nearestNeighbor
Use thenearestNeighbor query operator in YQL:
client/src/main/java/ai/vespa/client/dsl/Field.java):
Query Parameters
targetHits
Number of approximate nearest neighbors to return:targetHits controls the recall-performance tradeoff. Higher values improve recall but increase query latency.approximate
Control whether to use approximate or exact search:hnsw.exploreAdditionalHits
Fine-tune the search quality:Embedding Integration
Vespa can automatically generate embeddings using theEmbedder interface:
Schema Configuration
Query-Time Embedding
Embed query text automatically:input.query(query_embedding) parameter:
Hybrid Search
Combine vector search with text search and filters:Distance Calculation Features
Access distance scores in ranking expressions:closeness feature (searchlib/src/vespa/searchlib/features/closenessfeature.h) provides normalized distance scores.
Performance Tuning
Choose appropriate max-links-per-node
Start with 16, increase to 32 for better recall on large datasets
Use pre-normalized embeddings
When using
angular distance, normalize vectors and use prenormalized-angular for better performanceMemory Usage
HNSW index memory usage depends on:- Number of documents
- Vector dimensions
max-links-per-nodesetting
Best Practices
- Normalize embeddings when using angular distance
- Use appropriate distance metrics for your embedding model
- Start with default HNSW parameters and tune based on metrics
- Monitor recall metrics to ensure search quality
- Combine with filters for better result relevance
Next Steps
- Learn about Ranking Expressions to combine vector and text signals
- Explore Text Search for hybrid search
- Read about Grouping & Aggregation for result organization