VectorIndex

Trait VectorIndex 

Source
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§

Source

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.

Source

fn delete(&mut self, entity_id: EntityId) -> Result<bool, VectorError>

Remove an embedding from the index.

§Returns

Returns true if the embedding was removed, false if it didn’t exist.

§Errors

Returns an error if there’s a storage error.

Source

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 embedding
  • k - The number of nearest neighbors to return
  • ef_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.

Source

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,

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 embedding
  • k - The number of nearest neighbors to return
  • predicate - A function that returns true for entity IDs that should be included
  • ef_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.

Source

fn contains(&self, entity_id: EntityId) -> Result<bool, VectorError>

Check if an embedding exists in the index.

Source

fn len(&self) -> Result<usize, VectorError>

Get the number of embeddings in the index.

Source

fn dimension(&self) -> Result<usize, VectorError>

Get the dimension of embeddings in this index.

§Errors

Returns an error if there’s a concurrency error (lock poisoning).

Provided Methods§

Source

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.

Source

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.

Implementors§