pub struct FlatIndex { /* private fields */ }Expand description
Brute-force exact nearest-neighbour index.
See the crate-level docs for the design notes and the
iqdb_index::IndexCore / iqdb_index::Index contracts this type
satisfies.
§Examples
use std::sync::Arc;
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::{Index, IndexCore};
use iqdb_types::{DistanceMetric, SearchParams, VectorId};
let mut idx = FlatIndex::new(2, DistanceMetric::Euclidean, FlatConfig)?;
idx.insert(VectorId::from(1u64), Arc::<[f32]>::from(&[0.0, 0.0][..]), None)?;
idx.insert(VectorId::from(2u64), Arc::<[f32]>::from(&[3.0, 4.0][..]), None)?;
let hits = idx.search(&[0.0, 0.0], &SearchParams::new(1, DistanceMetric::Euclidean))?;
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, VectorId::U64(1));Implementations§
Source§impl FlatIndex
impl FlatIndex
Sourcepub fn new_unconfigured(dim: usize, metric: DistanceMetric) -> Result<Self>
pub fn new_unconfigured(dim: usize, metric: DistanceMetric) -> Result<Self>
Builds an empty index for dim-component vectors compared under
metric.
Returns IqdbError::InvalidConfig when dim == 0. This is the
same construction surface as Index::new; calling it directly
is the convenient path when the concrete type is known and there is
nothing to configure.
§Examples
use iqdb_flat::FlatIndex;
use iqdb_types::DistanceMetric;
let idx = FlatIndex::new_unconfigured(3, DistanceMetric::Cosine)?;
assert_eq!(idx.dim(), 3);
assert!(idx.is_empty());Sourcepub fn dim(&self) -> usize
pub fn dim(&self) -> usize
The dimensionality the index was built for.
§Examples
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::Index;
use iqdb_types::DistanceMetric;
let idx = FlatIndex::new(8, DistanceMetric::Euclidean, FlatConfig)?;
assert_eq!(idx.dim(), 8);Sourcepub fn metric(&self) -> DistanceMetric
pub fn metric(&self) -> DistanceMetric
The distance metric the index was built for.
§Examples
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::Index;
use iqdb_types::DistanceMetric;
let idx = FlatIndex::new(8, DistanceMetric::Cosine, FlatConfig)?;
assert_eq!(idx.metric(), DistanceMetric::Cosine);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of searchable vectors in the index.
§Examples
use std::sync::Arc;
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::{Index, IndexCore};
use iqdb_types::{DistanceMetric, VectorId};
let mut idx = FlatIndex::new(1, DistanceMetric::Euclidean, FlatConfig)?;
assert_eq!(idx.len(), 0);
idx.insert(VectorId::from(1u64), Arc::<[f32]>::from(&[0.0][..]), None)?;
assert_eq!(idx.len(), 1);Trait Implementations§
Source§impl IndexCore for FlatIndex
impl IndexCore for FlatIndex
Source§fn insert_batch(
&mut self,
items: Vec<(VectorId, Arc<[f32]>, Option<Metadata>)>,
) -> Result<()>
fn insert_batch( &mut self, items: Vec<(VectorId, Arc<[f32]>, Option<Metadata>)>, ) -> Result<()>
Reserves capacity for all backing stores up front, then inserts each
item via insert.
This is the same fail-fast contract as the trait default (the first
error returns immediately; inserts before it remain), but a single
reserve(items.len()) on each of the four Vecs and the
HashMap replaces the O(log n) incremental reallocations the
default loop would trigger — a measurable win for bulk loads.
Source§fn search(&self, query: &[f32], params: &SearchParams) -> Result<Vec<Hit>>
fn search(&self, query: &[f32], params: &SearchParams) -> Result<Vec<Hit>>
Searches for the top-k nearest neighbours under params.metric.
Returns IqdbError::DimensionMismatch if query.len() != self.dim,
IqdbError::InvalidMetric if the params metric does not match the
index’s, and IqdbError::InvalidFilter if params.filter is
supplied and rejected by FilterEvaluator::new (depth or In
cardinality past iqdb_filter’s MAX_FILTER_DEPTH /
MAX_IN_VALUES). A pathological filter surfaces as a clean error
rather than overflowing the search thread.
Source§fn insert(
&mut self,
id: VectorId,
vector: Arc<[f32]>,
metadata: Option<Metadata>,
) -> Result<()>
fn insert( &mut self, id: VectorId, vector: Arc<[f32]>, metadata: Option<Metadata>, ) -> Result<()>
Source§fn delete(&mut self, id: &VectorId) -> Result<()>
fn delete(&mut self, id: &VectorId) -> Result<()>
id from the search space. Read moreSource§fn metric(&self) -> DistanceMetric
fn metric(&self) -> DistanceMetric
Source§fn stats(&self) -> IndexStats
fn stats(&self) -> IndexStats
Auto Trait Implementations§
impl Freeze for FlatIndex
impl RefUnwindSafe for FlatIndex
impl Send for FlatIndex
impl Sync for FlatIndex
impl Unpin for FlatIndex
impl UnsafeUnpin for FlatIndex
impl UnwindSafe for FlatIndex
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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