pub struct ExactKnn { /* private fields */ }Expand description
Exact k-NN search operator using brute force.
Computes distances to all provided vectors and returns the K nearest. This is useful for:
- Small datasets where HNSW overhead isn’t justified
- Validating HNSW results
- Post-filtering a small candidate set from graph traversal
§Complexity
O(n * d) where n is the number of vectors and d is the dimension.
For large datasets, use AnnScan instead.
§Example
ⓘ
use manifoldb_vector::ops::{ExactKnn, VectorOperator, SearchConfig};
use manifoldb_vector::distance::DistanceMetric;
let vectors = vec![
(EntityId::new(1), embedding1),
(EntityId::new(2), embedding2),
];
let config = SearchConfig::k_nearest(5);
let mut knn = ExactKnn::new(vectors, query, DistanceMetric::Cosine, config)?;
while let Some(m) = knn.next()? {
println!("Entity {:?} at distance {}", m.entity_id, m.distance);
}Implementations§
Source§impl ExactKnn
impl ExactKnn
Sourcepub fn new<I>(
vectors: I,
query: &Embedding,
metric: DistanceMetric,
config: SearchConfig,
) -> Result<Self, VectorError>
pub fn new<I>( vectors: I, query: &Embedding, metric: DistanceMetric, config: SearchConfig, ) -> Result<Self, VectorError>
Sourcepub fn k_nearest<I>(
vectors: I,
query: &Embedding,
metric: DistanceMetric,
k: usize,
) -> Result<Self, VectorError>
pub fn k_nearest<I>( vectors: I, query: &Embedding, metric: DistanceMetric, k: usize, ) -> Result<Self, VectorError>
Create a k-nearest search from an iterator of vectors.
Convenience method for common case.
Sourcepub fn within_distance<I>(
vectors: I,
query: &Embedding,
metric: DistanceMetric,
max_distance: f32,
) -> Result<Self, VectorError>
pub fn within_distance<I>( vectors: I, query: &Embedding, metric: DistanceMetric, max_distance: f32, ) -> Result<Self, VectorError>
Create a within-distance search from an iterator of vectors.
Returns all vectors within the specified distance threshold.
Sourcepub fn from_slice(
vectors: &[(EntityId, Embedding)],
query: &Embedding,
metric: DistanceMetric,
config: SearchConfig,
) -> Result<Self, VectorError>
pub fn from_slice( vectors: &[(EntityId, Embedding)], query: &Embedding, metric: DistanceMetric, config: SearchConfig, ) -> Result<Self, VectorError>
Create from a slice of vectors (borrows and clones).
Useful when you have a reference to existing data.
Sourcepub fn peek(&self) -> Option<&VectorMatch>
pub fn peek(&self) -> Option<&VectorMatch>
Peek at the next result without consuming it.
Sourcepub fn as_slice(&self) -> &[VectorMatch]
pub fn as_slice(&self) -> &[VectorMatch]
Get all results as a slice.
Trait Implementations§
Source§impl VectorOperator for ExactKnn
impl VectorOperator for ExactKnn
Source§fn next(&mut self) -> Result<Option<VectorMatch>, VectorError>
fn next(&mut self) -> Result<Option<VectorMatch>, VectorError>
Get the next match from the operator. Read more
Source§fn collect_all(&mut self) -> Result<Vec<VectorMatch>, VectorError>
fn collect_all(&mut self) -> Result<Vec<VectorMatch>, VectorError>
Collect all remaining matches into a vector. Read more
Auto Trait Implementations§
impl Freeze for ExactKnn
impl RefUnwindSafe for ExactKnn
impl Send for ExactKnn
impl Sync for ExactKnn
impl Unpin for ExactKnn
impl UnwindSafe for ExactKnn
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
Mutably borrows from an owned value. Read more