Module hnsw

Module hnsw 

Source
Expand description

HNSW (Hierarchical Navigable Small World) Index

State-of-the-art approximate nearest neighbor (ANN) algorithm. Provides sub-linear search time with high recall.

§Algorithm Overview

HNSW builds a multi-layer graph where:

  • Layer 0 contains all vectors (densest)
  • Higher layers contain progressively fewer vectors (sparser)
  • Search starts from the top layer and navigates down

§Parameters

  • M: Maximum connections per node (default: 16)
  • ef_construction: Build-time quality (default: 200)
  • ef_search: Search-time quality vs speed trade-off (default: 50)

§Example

use oxify_vector::hnsw::{HnswIndex, HnswConfig};
use std::collections::HashMap;

// Create embeddings
let mut embeddings = HashMap::new();
embeddings.insert("doc1".to_string(), vec![0.1, 0.2, 0.3]);
embeddings.insert("doc2".to_string(), vec![0.2, 0.3, 0.4]);
embeddings.insert("doc3".to_string(), vec![0.9, 0.8, 0.7]);

// Build HNSW index
let config = HnswConfig::default();
let mut index = HnswIndex::new(config);
index.build(&embeddings)?;

// Search for similar documents
let query = vec![0.15, 0.25, 0.35];
let results = index.search(&query, 2)?;

for result in results {
    println!("{}: score = {:.4}", result.entity_id, result.score);
}

Structs§

HnswConfig
HNSW configuration
HnswIndex
HNSW Index for approximate nearest neighbor search
HnswStats
HNSW index statistics