Skip to content

ANNy - Very fast HNSW

Fast semantic search indices

Updated View as Markdown

Approximate Nearest Neighbors… yeah (ANNy)

This is a very fast crate for creating and searching HNSWs.

Let’s take a look at an example paired with ESE:

use anny::hnsw::Hnsw;
use anny::metric::Cosine;

const DIM: usize = ese::DIMENSIONS;

// Hnsw is parameterized entirely at compile time:
//   Dtype, Metric, DIM, M_0, K, EF_SEARCH, EF_BUILD, MAX_LEVEL
// here: ese embedding dim, cosine distance, return top-2 neighbors.
type Index = Hnsw<f32, Cosine, DIM, 32, 2, 64, 128, 16>;

fn main() {
    // every index starts empty
    let mut ix: Index = Hnsw::new(Cosine, 1);

    let docs = ["potato", "root vegetable", "spaceship"];
    for doc in docs {
        // insert returns a stable u32 id for the vector
        ix.insert(ese::encode_single(doc));
    }

    // search returns up to K nearest (distance, id), ascending by distance
    // (cosine distance: smaller = closer)
    let hits = ix.search(&ese::encode_single("tuber"));
    for (dist, id) in hits {
        println!("{dist:.3}  {}", docs[id as usize]);
    }
    // potato and root vegetable beat spaceship
}
Navigation

Type to search…