Trait usearch::VectorType
source · pub trait VectorType {
// Required methods
fn add(index: &Index, key: Key, vector: &[Self]) -> Result<(), Exception>
where Self: Sized;
fn get(
index: &Index,
key: Key,
buffer: &mut [Self]
) -> Result<usize, Exception>
where Self: Sized;
fn search(
index: &Index,
query: &[Self],
count: usize
) -> Result<Matches, Exception>
where Self: Sized;
fn filtered_search<F>(
index: &Index,
query: &[Self],
count: usize,
filter: F
) -> Result<Matches, Exception>
where Self: Sized,
F: Fn(Key) -> bool;
fn change_metric(
index: &mut Index,
metric: Box<dyn Fn(*const Self, *const Self) -> Distance + Send + Sync>
) -> Result<(), Exception>
where Self: Sized;
}
Expand description
The VectorType
trait defines operations for managing and querying vectors
in an index. It supports generic operations on vectors of different types,
allowing for the addition, retrieval, and search of vectors within an index.
Required Methods§
sourcefn add(index: &Index, key: Key, vector: &[Self]) -> Result<(), Exception>where
Self: Sized,
fn add(index: &Index, key: Key, vector: &[Self]) -> Result<(), Exception>where
Self: Sized,
Adds a vector to the index under the specified key.
§Parameters
index
: A reference to theIndex
where the vector is to be added.key
: The key under which the vector should be stored.vector
: A slice representing the vector to be added.
§Returns
Ok(())
if the vector was successfully added to the index.Err(cxx::Exception)
if an error occurred during the operation.
sourcefn get(index: &Index, key: Key, buffer: &mut [Self]) -> Result<usize, Exception>where
Self: Sized,
fn get(index: &Index, key: Key, buffer: &mut [Self]) -> Result<usize, Exception>where
Self: Sized,
Retrieves a vector from the index by its key.
§Parameters
index
: A reference to theIndex
from which the vector is to be retrieved.key
: The key of the vector to retrieve.buffer
: A mutable slice where the retrieved vector will be stored. The size of the buffer determines the maximum number of elements that can be retrieved.
§Returns
Ok(usize)
indicating the number of elements actually written into thebuffer
.Err(cxx::Exception)
if an error occurred during the operation.
sourcefn search(
index: &Index,
query: &[Self],
count: usize
) -> Result<Matches, Exception>where
Self: Sized,
fn search(
index: &Index,
query: &[Self],
count: usize
) -> Result<Matches, Exception>where
Self: Sized,
Performs a search in the index using the given query vector, returning
up to count
closest matches.
§Parameters
index
: A reference to theIndex
where the search is to be performed.query
: A slice representing the query vector.count
: The maximum number of matches to return.
§Returns
Ok(ffi::Matches)
containing the matches found.Err(cxx::Exception)
if an error occurred during the search operation.
sourcefn filtered_search<F>(
index: &Index,
query: &[Self],
count: usize,
filter: F
) -> Result<Matches, Exception>
fn filtered_search<F>( index: &Index, query: &[Self], count: usize, filter: F ) -> Result<Matches, Exception>
Performs a filtered search in the index using a query vector and a custom
filter function, returning up to count
matches that satisfy the filter.
§Parameters
index
: A reference to theIndex
where the search is to be performed.query
: A slice representing the query vector.count
: The maximum number of matches to return.filter
: A closure that takes aKey
and returnstrue
if the corresponding vector should be included in the search results, orfalse
otherwise.
§Returns
Ok(ffi::Matches)
containing the matches that satisfy the filter.Err(cxx::Exception)
if an error occurred during the filtered search operation.
sourcefn change_metric(
index: &mut Index,
metric: Box<dyn Fn(*const Self, *const Self) -> Distance + Send + Sync>
) -> Result<(), Exception>where
Self: Sized,
fn change_metric(
index: &mut Index,
metric: Box<dyn Fn(*const Self, *const Self) -> Distance + Send + Sync>
) -> Result<(), Exception>where
Self: Sized,
Changes the metric used for distance calculations within the index.
§Parameters
index
: A mutable reference to theIndex
for which the metric is to be changed.metric
: A boxed closure that defines the new metric for distance calculation. The closure must take two pointers to elements of typeSelf
and return aDistance
.
§Returns
Ok(())
if the metric was successfully changed.Err(cxx::Exception)
if an error occurred during the operation.