ExactKnn

Struct ExactKnn 

Source
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

Source

pub fn new<I>( vectors: I, query: &Embedding, metric: DistanceMetric, config: SearchConfig, ) -> Result<Self, VectorError>
where I: IntoIterator<Item = (EntityId, Embedding)>,

Create a new exact k-NN search operator.

§Arguments
  • vectors - Iterator of (entity_id, embedding) pairs to search
  • query - The query embedding
  • metric - Distance metric to use
  • config - Search configuration
§Errors

Returns an error if any vector has a dimension mismatch with the query.

Source

pub fn k_nearest<I>( vectors: I, query: &Embedding, metric: DistanceMetric, k: usize, ) -> Result<Self, VectorError>
where I: IntoIterator<Item = (EntityId, Embedding)>,

Create a k-nearest search from an iterator of vectors.

Convenience method for common case.

Source

pub fn within_distance<I>( vectors: I, query: &Embedding, metric: DistanceMetric, max_distance: f32, ) -> Result<Self, VectorError>
where I: IntoIterator<Item = (EntityId, Embedding)>,

Create a within-distance search from an iterator of vectors.

Returns all vectors within the specified distance threshold.

Source

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.

Source

pub fn len(&self) -> usize

Get the number of results found.

Source

pub fn is_empty(&self) -> bool

Check if no results were found.

Source

pub fn peek(&self) -> Option<&VectorMatch>

Peek at the next result without consuming it.

Source

pub fn reset(&mut self)

Reset the iterator to the beginning.

Source

pub fn as_slice(&self) -> &[VectorMatch]

Get all results as a slice.

Trait Implementations§

Source§

impl VectorOperator for ExactKnn

Source§

fn next(&mut self) -> Result<Option<VectorMatch>, VectorError>

Get the next match from the operator. Read more
Source§

fn dimension(&self) -> usize

Get the dimension of vectors this operator works with.
Source§

fn collect_all(&mut self) -> Result<Vec<VectorMatch>, VectorError>

Collect all remaining matches into a vector. Read more

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<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, 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<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