pub struct HnswIndex { /* private fields */ }Expand description
Main HNSW vector search index
Provides approximate nearest neighbor search capabilities using the Hierarchical Navigable Small World algorithm. Integrates with SQLiteGraph to provide vector-augmented graph queries.
§Performance Characteristics
- Search Time: O(log N) average case complexity
- Memory Usage: 2-3x vector data size overhead
- Build Time: O(N log N) with construction parameters
- Accuracy: 95%+ recall for typical workloads
Implementations§
§impl HnswIndex
impl HnswIndex
pub fn new(name: &str, config: HnswConfig) -> Result<Self, HnswError>
pub fn new(name: &str, config: HnswConfig) -> Result<Self, HnswError>
Create a new HNSW index with the specified configuration
§Arguments
name- Name of the index (for persistence and multi-index support)config- HNSW configuration parameters
§Returns
Returns a new HnswIndex ready for vector insertion and search
§Examples
use sqlitegraph::hnsw::{HnswIndex, HnswConfig, DistanceMetric};
let config = HnswConfig::builder()
.dimension(128)
.distance_metric(DistanceMetric::Euclidean)
.build()?;
let hnsw = HnswIndex::new("my_index", config)?;pub fn with_persistent_storage(
name: &str,
config: HnswConfig,
conn: Connection,
) -> Result<Self, HnswError>
pub fn with_persistent_storage( name: &str, config: HnswConfig, conn: Connection, ) -> Result<Self, HnswError>
Create a new HNSW index with SQLite-backed persistent storage
§Arguments
name- Name of the indexconfig- HNSW configuration parametersconn- SQLite connection
§Returns
Returns a new HnswIndex with SQLite storage
§Note
This creates an index with persistent storage. The index_id will be set after saving metadata to the database.
pub fn with_storage(
name: &str,
config: HnswConfig,
storage: Box<dyn VectorStorage>,
) -> Result<Self, HnswError>
pub fn with_storage( name: &str, config: HnswConfig, storage: Box<dyn VectorStorage>, ) -> Result<Self, HnswError>
pub fn insert_vector(
&mut self,
vector: &[f32],
metadata: Option<Value>,
) -> Result<u64, HnswError>
pub fn insert_vector( &mut self, vector: &[f32], metadata: Option<Value>, ) -> Result<u64, HnswError>
Insert a vector into the HNSW index
§Arguments
vector- Vector data to insert (must match configured dimension)metadata- Optional JSON metadata to associate with the vector
§Returns
Returns the assigned vector ID for future reference
§Errors
Returns HnswError::Index for dimension mismatches or insert failures
§Examples
let vector = vec![1.0, 0.0, 0.0];
let metadata = serde_json::json!({"label": "test"});
let vector_id = hnsw.insert_vector(&vector, Some(metadata))?;
println!("Inserted vector with ID: {}", vector_id);pub fn search(
&self,
query: &[f32],
k: usize,
) -> Result<Vec<(u64, f32)>, HnswError>
pub fn search( &self, query: &[f32], k: usize, ) -> Result<Vec<(u64, f32)>, HnswError>
Search for the k nearest neighbors to a query vector
§Arguments
query- Query vector (must match configured dimension)k- Number of nearest neighbors to return
§Returns
Returns a vector of (vector_id, distance) tuples sorted by distance
§Errors
Returns HnswError::Index for dimension mismatches or search failures
§Examples
let query = vec![1.0, 0.0, 0.0];
let results = hnsw.search(&query, 5)?;
for (id, distance) in results {
println!("Vector {}: distance {}", id, distance);
}pub fn statistics(&self) -> Result<HnswIndexStats, HnswError>
pub fn statistics(&self) -> Result<HnswIndexStats, HnswError>
pub fn vector_count(&self) -> usize
pub fn vector_count(&self) -> usize
Get the number of vectors in this index
pub fn config(&self) -> &HnswConfig
pub fn config(&self) -> &HnswConfig
Get the HNSW configuration
Returns a reference to the index configuration
§impl HnswIndex
impl HnswIndex
pub fn save_metadata(&self, conn: &Connection) -> Result<(), HnswError>
pub fn save_metadata(&self, conn: &Connection) -> Result<(), HnswError>
pub fn load_metadata(conn: &Connection, name: &str) -> Result<Self, HnswError>
pub fn load_metadata(conn: &Connection, name: &str) -> Result<Self, HnswError>
pub fn load_vectors_and_rebuild(
&mut self,
conn: &Connection,
) -> Result<(), HnswError>
pub fn load_vectors_and_rebuild( &mut self, conn: &Connection, ) -> Result<(), HnswError>
Load all vectors from database and rebuild HNSW index
§Arguments
conn- SQLite connection
§Returns
Ok(()) if successful
§Errors
Returns HnswError on database failure or vector loading errors
§Note
This method loads all vectors from the database and rebuilds the HNSW graph structure by inserting each vector. The O(N log N) rebuild cost is a trade-off for simpler implementation compared to persisting layers.
pub fn load_with_vectors(
conn: &Connection,
name: &str,
) -> Result<Self, HnswError>
pub fn load_with_vectors( conn: &Connection, name: &str, ) -> Result<Self, HnswError>
pub fn list_indexes(conn: &Connection) -> Result<Vec<String>, HnswError>
pub fn list_indexes(conn: &Connection) -> Result<Vec<String>, HnswError>
pub fn delete_index(conn: &Connection, name: &str) -> Result<(), HnswError>
pub fn delete_index(conn: &Connection, name: &str) -> Result<(), HnswError>
Auto Trait Implementations§
impl Freeze for HnswIndex
impl !RefUnwindSafe for HnswIndex
impl !Send for HnswIndex
impl !Sync for HnswIndex
impl Unpin for HnswIndex
impl !UnwindSafe for HnswIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more