Skip to main content

Crate fast_hnsw

Crate fast_hnsw 

Source
Expand description

§hnsw

A pure-Rust implementation of Hierarchical Navigable Small World (HNSW) approximate nearest-neighbour search, following the algorithm from:

Malkov & Yashunin, “Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs”, IEEE TPAMI 2018.

§Quick start

use hnsw::{Builder, Hnsw, SearchResult};
use hnsw::distance::Euclidean;

// Build an index.
let mut index: Hnsw<Euclidean> = Builder::new()
    .m(16)
    .ef_construction(200)
    .seed(42)
    .build(Euclidean);

// Insert vectors.
for i in 0..100_u32 {
    index.insert(vec![i as f32, (i * i) as f32]);
}

// Query: find 5 nearest neighbours with ef=50.
let results: Vec<SearchResult> = index.search(&[10.0, 101.0], 5, 50);
assert_eq!(results[0].id, 10);

§Distance metrics

TypeDescription
distance::EuclideanTrue L2 distance
distance::SquaredEuclideanL2² (faster, same NN order)
distance::Cosine1 − cosine similarity
distance::DotProduct1 − dot product
distance::ManhattanL1 / taxicab distance

Custom metrics are easy to add by implementing the distance::Distance trait.

§Feature flags

(none yet — this crate is dependency-light by design)

Re-exports§

pub use builder::Builder;
pub use hnsw::Config;
pub use hnsw::Hnsw;
pub use hnsw::IndexStats;
pub use hnsw::PruneStrategy;
pub use hnsw::SearchResult;
pub use labeled::LabeledIndex;
pub use paired::PairedIndex;

Modules§

builder
Ergonomic builder for Hnsw.
distance
hnsw
Core HNSW index — optimised implementation.
labeled
Single-embedding index with typed per-vector payload.
paired
Dual-embedding index: two HNSW graphs over the same items.
payload
Typed payload stored alongside each vector in a [LabeledIndex] or [PairedIndex].
persist
Binary file format and memory-mapped loading for Hnsw indexes.