VectorIndex

Struct VectorIndex 

Source
pub struct VectorIndex { /* private fields */ }
Expand description

HNSW-based vector index for semantic search

Provides efficient approximate k-nearest neighbor search over high-dimensional vectors associated with content IDs.

Implementations§

Source§

impl VectorIndex

Source

pub fn new( dimension: usize, metric: DistanceMetric, max_nb_connection: usize, ef_construction: usize, ) -> Result<Self>

Create a new vector index with the specified dimension

§Arguments
  • dimension - Dimension of vectors to be indexed
  • metric - Distance metric to use
  • max_nb_connection - Maximum number of connections per layer (M parameter)
  • ef_construction - Size of dynamic candidate list (efConstruction parameter)
Source

pub fn with_defaults(dimension: usize) -> Result<Self>

Create a new index with default parameters

Uses M=16 and efConstruction=200, which are good defaults for most use cases

Source

pub fn insert(&mut self, cid: &Cid, vector: &[f32]) -> Result<()>

Insert a vector associated with a CID

§Arguments
  • cid - Content identifier
  • vector - Feature vector to index
Source

pub fn search( &self, query: &[f32], k: usize, ef_search: usize, ) -> Result<Vec<SearchResult>>

Search for k nearest neighbors

§Arguments
  • query - Query vector
  • k - Number of neighbors to return
  • ef_search - Size of dynamic candidate list during search (higher = more accurate but slower)
Source

pub fn delete(&mut self, cid: &Cid) -> Result<()>

Delete a vector by CID

Source

pub fn contains(&self, cid: &Cid) -> bool

Check if a CID exists in the index

Source

pub fn len(&self) -> usize

Get the number of vectors in the index

Source

pub fn is_empty(&self) -> bool

Check if the index is empty

Source

pub fn dimension(&self) -> usize

Get the dimension of vectors in this index

Source

pub fn metric(&self) -> DistanceMetric

Get the distance metric used by this index

Source

pub fn get_all_cids(&self) -> Vec<Cid>

Get all CIDs in the index Useful for synchronization and snapshots

Source

pub fn get_embedding(&self, cid: &Cid) -> Option<Vec<f32>>

Get the embedding vector for a specific CID

Returns None if the CID is not in the index

Source

pub fn get_all_embeddings(&self) -> Vec<(Cid, Vec<f32>)>

Get all embeddings in the index as (CID, vector) pairs

Useful for iteration, migration, and batch operations

Source

pub fn iter(&self) -> Vec<(Cid, Vec<f32>)>

Iterate over all (CID, vector) pairs in the index

Returns an iterator over the embeddings

Source

pub fn compute_optimal_parameters(&self) -> (usize, usize)

Compute optimal HNSW parameters based on current index size

Returns recommended (max_nb_connection, ef_construction) based on:

  • Small indexes (< 10k): M=16, ef=200
  • Medium indexes (10k-100k): M=32, ef=400
  • Large indexes (> 100k): M=48, ef=600

Get recommended ef_search parameter based on k

Generally ef_search should be >= k and higher for better recall

Source

pub fn get_parameter_recommendations( &self, use_case: UseCase, ) -> ParameterRecommendation

Get detailed parameter recommendations based on use case

Source

pub fn insert_batch(&mut self, items: &[(Cid, Vec<f32>)]) -> Result<()>

Insert multiple vectors in batch

More efficient than inserting one by one as it can use parallelization

§Arguments
  • items - Vector of (CID, vector) pairs to insert
Source

pub fn insert_incremental( &mut self, items: &[(Cid, Vec<f32>)], chunk_size: usize, ) -> Result<IncrementalBuildStats>

Insert vectors incrementally with periodic optimization

This method inserts vectors in chunks and tracks statistics to determine if index rebuild is beneficial. Returns statistics about the insertion.

§Arguments
  • items - Vector of (CID, vector) pairs to insert
  • chunk_size - Number of vectors to insert before checking optimization
§Returns

Statistics about the incremental build process

Source

pub fn should_rebuild(&self) -> bool

Determine if index should be rebuilt for better performance

Rebuild is recommended when:

  • Index has grown significantly (2x or more)
  • Many deletions have occurred (fragmentation)
  • Current parameters are suboptimal for index size
Source

pub fn rebuild(&mut self, use_case: UseCase) -> Result<RebuildStats>

Rebuild the index with optimal parameters for current size

This creates a new index with better parameters and re-inserts all vectors. Use this when should_rebuild() returns true.

§Arguments
  • use_case - Target use case for parameter selection
Source

pub fn get_build_stats(&self) -> BuildHealthStats

Get statistics about incremental build performance

Source

pub fn save(&self, path: impl AsRef<Path>) -> Result<()>

Save the index to a file

Saves the HNSW index and CID mappings to disk for later retrieval. The index is saved in oxicode format.

§Arguments
  • path - Path to save the index to
Source

pub fn load(path: impl AsRef<Path>) -> Result<Self>

Load an index from a file

Loads a previously saved index from disk.

§Arguments
  • path - Path to load the index from

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,