Overview
TheHNSWIndex struct implements the Hierarchical Navigable Small World graph algorithm for approximate nearest neighbor (ANN) search. This is the performance core of SolVec, providing fast and accurate vector similarity search.
Parameters
M
Max connections per node per layer (default: 16)
ef_construction
Beam width during index build (default: 200)
ef_search
Beam width during query (default: 50)
Type Signature
Constructor Methods
new
Create a new HNSW index with custom parameters.Max connections per node. 16 is standard. Higher = better recall, more memory.
Build-time beam width. 200 is standard. Higher = better quality, slower build.
Distance metric for similarity computation. Options:
Cosine, Euclidean, DotProduct.default_cosine
Create an index with sensible defaults — what most users should use.Returns an index with M=16, ef_construction=200, and Cosine metric.
Core Methods
insert
Insert a vector into the index. If a vector with the same ID already exists, it is updated.The vector to insert. Must have a unique ID and non-empty values.
Returns
Ok(()) on success, or SolVecError::DimensionMismatch if the vector dimension doesn’t match the index dimension.query
Query the index for top-K nearest neighbors.The query vector to search for.
Number of nearest neighbors to return. Must be >= 1.
Returns results sorted by score descending (most similar first). Each
QueryResult contains:id: Vector IDscore: Similarity score (higher = more similar)metadata: Associated metadata
delete
Delete a vector by ID.The ID of the vector to delete.
Returns
Ok(()) on success, or SolVecError::VectorNotFound if the vector doesn’t exist.update
Update a vector (convenience wrapper for delete + insert).The updated vector with the same ID.
Configuration Methods
set_ef_search
Set ef_search parameter — increase for better recall at cost of speed.The new ef_search value (minimum: 1).
Query Methods
len
Number of vectors in the index.The total number of vectors currently indexed.
is_empty
Whether the index contains no vectors.Returns
true if the index is empty, false otherwise.metric
The distance metric used by this index.Returns the distance metric:
Cosine, Euclidean, or DotProduct.stats
Get detailed statistics about the index.Returns an
IndexStats struct containing:vector_count: Number of vectorslayer_count: Number of HNSW layersentry_point_level: Top layer leveldimension: Vector dimensiontotal_inserts: Total insertions (including updates)total_deletes: Total deletionsmetric: Distance metric
Serialization Methods
to_json
Serialize the entire index to JSON for persistence.Returns JSON string representation of the index, or
SolVecError::SerializationError on failure.from_json
Deserialize an index from JSON.JSON string representation of an index.
Returns the deserialized index, or
SolVecError::SerializationError on failure.Complete Example
IndexStats Struct
Error Handling
All methods that can fail returnResult<T, SolVecError>. Common errors:
DimensionMismatch: Vector dimension doesn’t match index dimensionVectorNotFound: Attempted to delete a non-existent vectorInvalidVector: Vector has empty ID or contains NaN/infinite valuesInvalidTopK: top_k parameter is 0SerializationError: Failed to serialize/deserialize index
Performance Characteristics
- Insert: O(log N) on average
- Query: O(log N) on average
- Delete: O(M * log N) where M is max connections
- Space: O(N * M) where N is number of vectors
See Also
- Distance Metrics - Distance computation functions
- Merkle Tree - Cryptographic verification
- Encryption - Vector encryption