pub struct VectorIndex { /* private fields */ }Expand description
Vector index backed by HNSW (Hierarchical Navigable Small World) graphs.
Uses hnsw_rs for O(log N) approximate nearest-neighbour search.
Deletions are handled via a soft-delete set that filters results;
the index is compacted (rebuilt) when the deletion ratio exceeds 30%.
Implementations§
Source§impl VectorIndex
impl VectorIndex
Sourcepub fn new(dimension: usize) -> Self
pub fn new(dimension: usize) -> Self
Create a new, empty index for vectors of the given dimension.
Sourcepub fn add(&mut self, chunk_id: String, embedding: Vec<f32>) -> Result<()>
pub fn add(&mut self, chunk_id: String, embedding: Vec<f32>) -> Result<()>
Add an embedding to the index.
The embedding is L2-normalized at insert time so that the HNSW dot-product distance equals cosine distance.
Sourcepub fn remove(&mut self, chunk_id: &str)
pub fn remove(&mut self, chunk_id: &str)
Remove an embedding by chunk ID.
The entry is soft-deleted (filtered from search results).
When the fraction of deleted entries exceeds 30 %, the HNSW
graph is compacted automatically on the next search().
Sourcepub fn search(&self, query: &[f32], k: usize) -> Vec<(String, f32)>
pub fn search(&self, query: &[f32], k: usize) -> Vec<(String, f32)>
Search for the k most similar embeddings to query.
Returns (chunk_id, cosine_similarity) pairs sorted by
descending similarity.
Sourcepub fn compact(&mut self)
pub fn compact(&mut self)
Compact the index: rebuild the HNSW graph, dropping deleted entries.
Called automatically when the deletion ratio exceeds the threshold, but can also be invoked manually.
Sourcepub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32
Cosine similarity between two arbitrary vectors.
Normalizes both inputs before computing the dot product.
Sourcepub fn verify_index_integrity(&self) -> Vec<String>
pub fn verify_index_integrity(&self) -> Vec<String>
Verify index integrity, returning a list of issues found.
Checks for:
- Mismatched embedding dimensions
- NaN or Inf values in vectors
- Duplicate chunk IDs
- Empty embedding vectors
Sourcepub fn check_health(&self) -> IndexHealth
pub fn check_health(&self) -> IndexHealth
Check overall health of the index.
Auto Trait Implementations§
impl !RefUnwindSafe for VectorIndex
impl !UnwindSafe for VectorIndex
impl Freeze for VectorIndex
impl Send for VectorIndex
impl Sync for VectorIndex
impl Unpin for VectorIndex
impl UnsafeUnpin for VectorIndex
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request