Overview
TheHnswIndexParam class configures an HNSW (Hierarchical Navigable Small World) index. HNSW is a graph-based approximate nearest neighbor search algorithm that offers excellent performance and accuracy for high-dimensional vector search.
Constructor
Parameters
Distance metric used for similarity computation. Options:
MetricType.IP- Inner product (default)MetricType.L2- Euclidean distanceMetricType.COSINE- Cosine similarity
Number of bi-directional links created for every new element during construction. This is the most important parameter for HNSW.Effects:
- Higher values improve search accuracy and recall
- Higher values increase memory usage and construction time
- Typical range: 8-64
- Default of 50 works well for most use cases
Size of the dynamic candidate list for nearest neighbors during index construction. Controls the quality of the graph structure.Effects:
- Larger values yield better graph quality and search accuracy
- Larger values increase index construction time
- Should be at least as large as
m, typically much larger - Typical range: 100-1000
Optional quantization type for vector compression. Options:
QuantizeType.UNDEFINED- No quantization (default)QuantizeType.FP16- 16-bit floating pointQuantizeType.INT8- 8-bit integer quantization
Properties
m
Maximum number of neighbors per node in upper layers of the graph. Type:int
ef_construction
Candidate list size during index construction. Type:int
Methods
to_dict()
Convert the index parameters to a dictionary representation. Returns:dict - Dictionary with all index parameter fields.
Examples
Basic HNSW index
High accuracy configuration
Memory-efficient configuration
Fast construction configuration
Using with a collection
Performance Tuning
Parameter relationships:
ef_constructionshould typically be 5-20x larger thanm- Higher
mincreases memory usage by approximatelym * 4 * dimensionbytes per vector - Construction time increases roughly linearly with
ef_construction
Memory Usage
Approximate memory per vector:bytes_per_element= 4 for FLOAT32, 2 for FP16, 1 for INT8mis the HNSW parameter
When to Use HNSW
HNSW is ideal for:
- High-dimensional vectors (100+ dimensions)
- Scenarios requiring high recall (>95%)
- Real-time search applications
- When you have sufficient memory
See Also
- HnswQueryParam - Query parameters for HNSW searches
- IVFIndexParam - Alternative index type for large datasets
- FlatIndexParam - Exact search alternative