aprender/index/mod.rs
1//! Indexing data structures for efficient nearest neighbor search.
2//!
3//! This module provides approximate nearest neighbor search algorithms
4//! optimized for production ML workloads.
5//!
6//! # Algorithms
7//!
8//! - **HNSW** (Hierarchical Navigable Small World): O(log n) approximate search
9//!
10//! # Quick Start
11//!
12//! ```
13//! use aprender::index::hnsw::HNSWIndex;
14//! use aprender::primitives::Vector;
15//!
16//! // Create index with M=16 connections per node
17//! let mut index = HNSWIndex::new(16, 200, 0.0);
18//!
19//! // Add vectors at different angles (cosine distance measures angle)
20//! index.add("horizontal", Vector::from_slice(&[1.0, 0.0, 0.0]));
21//! index.add("diagonal", Vector::from_slice(&[1.0, 1.0, 0.0]));
22//! index.add("vertical", Vector::from_slice(&[0.0, 1.0, 0.0]));
23//!
24//! // Search for 2 nearest neighbors to nearly horizontal vector
25//! let query = Vector::from_slice(&[0.9, 0.1, 0.0]);
26//! let results = index.search(&query, 2);
27//!
28//! assert_eq!(results.len(), 2);
29//! // Results are sorted by cosine distance (closest first)
30//! assert!(results[0].1 <= results[1].1);
31//! ```
32
33pub mod hnsw;
34
35pub use hnsw::HNSWIndex;