Anda-DB HNSW Vector Search Library
A high-performance implementation of Hierarchical Navigable Small World (HNSW) algorithm for approximate nearest neighbor search in high-dimensional spaces.
Features
- Fast approximate nearest neighbor search
- Multiple distance metrics (Euclidean, Cosine, Inner Product, Manhattan)
- Configurable index parameters
- Thread-safe implementation with concurrent read/write operations
- Serialization and deserialization support
- Support for bf16 (brain floating point) vector storage for memory efficiency
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
Quick Start
use ;
// Create a new index for 384-dimensional vectors
const DIM: usize = 384;
let config = default;
let index = new;
// Insert vectors
let vector1: = vec!; // 384 dimensions
index.insert_f32?;
// Search for nearest neighbors
let query: = vec!; // 384 dimensions
let results = index.search_f32?;
// Results contain (id, distance) pairs
for in results
// Save index to file
let file = create?;
index.save.await?;
// Load index from file
let file = open?;
let loaded_index = load.await?;
Advanced Usage
Custom Configuration
use ;
let config = HnswConfig ;
let index = new;
Distance Metrics
The library supports multiple distance metrics:
// Euclidean distance (L2 norm)
let config = HnswConfig ;
// Cosine distance (1 - cosine similarity)
let config = HnswConfig ;
// Inner product (negative dot product)
let config = HnswConfig ;
// Manhattan distance (L1 norm)
let config = HnswConfig ;
Removing Vectors
// Remove a vector by ID
let removed = index.remove?;
Index Statistics
// Get index statistics
let stats = index.stats;
println!;
println!;
println!;
println!;
println!;
println!;
Example
Here's a complete example demonstrating vector insertion, search, and deletion:
use ;
use Rng;
use time;
async
Error Handling
The library uses a custom error type HnswError for various error conditions:
HnswError::Db: Database-related errorsHnswError::Cbor: Serialization/deserialization errorsHnswError::DimensionMismatch: When vector dimensions don't match the indexHnswError::EmptyIndex: When trying to search an empty indexHnswError::IndexFull: When index has reached its maximum capacityHnswError::NotFound: When a vector with the specified ID is not foundHnswError::AlreadyExists: When trying to add a vector with an ID that already existsHnswError::DistanceMetric: Errors related to distance calculations
Performance Considerations
- The HNSW algorithm provides logarithmic search complexity with respect to the number of vectors
- The
ef_searchparameter controls the trade-off between search speed and accuracy - The
ef_constructionparameter affects build time and quality of the graph structure - The
SelectNeighborsStrategy::Heuristicoption provides better search quality at the cost of longer index construction time - Using bf16 format for vector storage significantly reduces memory usage with minimal precision loss
License
Copyright © 2025 LDC Labs.
ldclabs/anda-db is licensed under the MIT License. See LICENSE for the full license text.