pub trait VectorIndex {
// Required methods
fn insert(
&mut self,
entity_id: EntityId,
embedding: &Embedding,
) -> Result<(), VectorError>;
fn delete(&mut self, entity_id: EntityId) -> Result<bool, VectorError>;
fn search(
&self,
query: &Embedding,
k: usize,
ef_search: Option<usize>,
) -> Result<Vec<SearchResult>, VectorError>;
fn search_with_filter<F>(
&self,
query: &Embedding,
k: usize,
predicate: F,
ef_search: Option<usize>,
config: Option<FilteredSearchConfig>,
) -> Result<Vec<SearchResult>, VectorError>
where F: Fn(EntityId) -> bool;
fn contains(&self, entity_id: EntityId) -> Result<bool, VectorError>;
fn len(&self) -> Result<usize, VectorError>;
fn dimension(&self) -> Result<usize, VectorError>;
// Provided methods
fn insert_batch(
&mut self,
embeddings: &[(EntityId, &Embedding)],
) -> Result<(), VectorError> { ... }
fn is_empty(&self) -> Result<bool, VectorError> { ... }
}Expand description
Trait for vector similarity search indexes.
This trait defines the interface for approximate nearest neighbor (ANN) indexes that support insert, delete, and k-NN search operations.
Required Methods§
Sourcefn insert(
&mut self,
entity_id: EntityId,
embedding: &Embedding,
) -> Result<(), VectorError>
fn insert( &mut self, entity_id: EntityId, embedding: &Embedding, ) -> Result<(), VectorError>
Insert an embedding into the index.
If an embedding already exists for the given entity, it will be updated.
§Errors
Returns an error if the embedding dimension doesn’t match the index dimension, or if there’s a storage error.
Sourcefn search(
&self,
query: &Embedding,
k: usize,
ef_search: Option<usize>,
) -> Result<Vec<SearchResult>, VectorError>
fn search( &self, query: &Embedding, k: usize, ef_search: Option<usize>, ) -> Result<Vec<SearchResult>, VectorError>
Search for the k nearest neighbors of a query vector.
§Arguments
query- The query embeddingk- The number of nearest neighbors to returnef_search- Optional beam width for search (uses default if None)
§Returns
A vector of search results, sorted by distance (closest first).
§Errors
Returns an error if the query dimension doesn’t match the index dimension, or if there’s a storage error.
Sourcefn search_with_filter<F>(
&self,
query: &Embedding,
k: usize,
predicate: F,
ef_search: Option<usize>,
config: Option<FilteredSearchConfig>,
) -> Result<Vec<SearchResult>, VectorError>
fn search_with_filter<F>( &self, query: &Embedding, k: usize, predicate: F, ef_search: Option<usize>, config: Option<FilteredSearchConfig>, ) -> Result<Vec<SearchResult>, VectorError>
Search for the k nearest neighbors with a filter predicate.
This performs filtered search where the predicate is applied during graph traversal, not as a post-filter. This is more efficient when the filter is selective, as it avoids exploring many non-matching paths.
§Arguments
query- The query embeddingk- The number of nearest neighbors to returnpredicate- A function that returns true for entity IDs that should be includedef_search- Optional beam width for search (uses adjusted default if None)config- Optional configuration for filtered search (uses defaults if None)
§Returns
A vector of search results that pass the predicate, sorted by distance (closest first).
§Errors
Returns an error if the query dimension doesn’t match the index dimension, or if there’s a storage error.
Sourcefn contains(&self, entity_id: EntityId) -> Result<bool, VectorError>
fn contains(&self, entity_id: EntityId) -> Result<bool, VectorError>
Check if an embedding exists in the index.
Sourcefn len(&self) -> Result<usize, VectorError>
fn len(&self) -> Result<usize, VectorError>
Get the number of embeddings in the index.
Provided Methods§
Sourcefn insert_batch(
&mut self,
embeddings: &[(EntityId, &Embedding)],
) -> Result<(), VectorError>
fn insert_batch( &mut self, embeddings: &[(EntityId, &Embedding)], ) -> Result<(), VectorError>
Insert multiple embeddings into the index in a batch operation.
This is significantly more efficient than calling insert multiple times
as it minimizes transaction overhead and optimizes HNSW graph construction.
All embeddings are inserted within a single operation.
If an embedding already exists for any entity, it will be updated.
§Performance
For bulk loading, this can provide up to 10x throughput improvement over
individual insert calls.
§Arguments
embeddings- Slice of (EntityId, Embedding reference) pairs to insert
§Errors
Returns an error if any embedding dimension doesn’t match the index dimension, or if there’s a storage error.
Sourcefn is_empty(&self) -> Result<bool, VectorError>
fn is_empty(&self) -> Result<bool, VectorError>
Check if the index is empty.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.