pub struct AnnScan<'a, E: StorageEngine> { /* private fields */ }Expand description
Approximate nearest neighbor search operator.
Uses an HNSW index to efficiently find the K nearest neighbors to a query vector. The search is approximate, trading some accuracy for speed.
§Example
use manifoldb_vector::ops::{AnnScan, VectorOperator, SearchConfig};
let config = SearchConfig::k_nearest(10).with_ef_search(50);
let mut scan = AnnScan::new(&index, query, config)?;
while let Some(m) = scan.next()? {
println!("Entity {:?} at distance {}", m.entity_id, m.distance);
}Implementations§
Source§impl<'a, E: StorageEngine> AnnScan<'a, E>
impl<'a, E: StorageEngine> AnnScan<'a, E>
Sourcepub fn new(
index: &'a HnswIndex<E>,
query: &Embedding,
config: SearchConfig,
) -> Result<Self, VectorError>
pub fn new( index: &'a HnswIndex<E>, query: &Embedding, config: SearchConfig, ) -> Result<Self, VectorError>
Create a new ANN scan operator.
Performs the HNSW search immediately and buffers the results for iteration. This is because HNSW search is most efficient when performed as a single operation.
§Arguments
index- Reference to the HNSW indexquery- The query embeddingconfig- Search configuration (k,max_distance,ef_search)
§Errors
Returns an error if the query dimension doesn’t match the index.
Sourcepub fn k_nearest(
index: &'a HnswIndex<E>,
query: &Embedding,
k: usize,
) -> Result<Self, VectorError>
pub fn k_nearest( index: &'a HnswIndex<E>, query: &Embedding, k: usize, ) -> Result<Self, VectorError>
Create an ANN scan with simple k-nearest configuration.
Convenience method for common case of finding K nearest neighbors.
§Arguments
index- Reference to the HNSW indexquery- The query embeddingk- Number of nearest neighbors to find
§Errors
Returns an error if the query dimension doesn’t match the index.
Sourcepub fn within_distance(
index: &'a HnswIndex<E>,
query: &Embedding,
max_distance: f32,
max_results: usize,
) -> Result<Self, VectorError>
pub fn within_distance( index: &'a HnswIndex<E>, query: &Embedding, max_distance: f32, max_results: usize, ) -> Result<Self, VectorError>
Create an ANN scan to find vectors within a distance threshold.
Note: HNSW is optimized for k-NN search. For within-distance queries,
this searches for a large k and filters by distance. Consider using
ExactKnn for precise distance-based filtering on small sets.
§Arguments
index- Reference to the HNSW indexquery- The query embeddingmax_distance- Maximum distance thresholdmax_results- Maximum number of results to return
§Errors
Returns an error if the query dimension doesn’t match the index.
Sourcepub fn peek(&self) -> Option<&VectorMatch>
pub fn peek(&self) -> Option<&VectorMatch>
Peek at the next result without consuming it.