pub enum DistanceMetric {
Cosine,
Euclidean,
DotProduct,
Manhattan,
}Expand description
Distance metric for vector similarity calculations.
The choice of distance metric significantly affects search results:
- Cosine: Best for normalized embeddings (most LLM embeddings).
- Euclidean: Best for raw feature vectors where magnitude matters.
- DotProduct: Best for vectors that are already normalized.
- Manhattan: Robust to outliers, good for sparse vectors.
Variants§
Cosine
Cosine similarity (1 - cosine_distance).
Measures the angle between vectors, ignoring magnitude. Range: [-1, 1], where 1 means identical direction.
Best for: Text embeddings, semantic similarity.
Euclidean
Euclidean (L2) distance.
Measures the straight-line distance between vectors. Range: [0, ∞), where 0 means identical vectors.
Best for: Image features, geographic coordinates.
DotProduct
Dot product (inner product).
Measures alignment of vectors including magnitude. Range: (-∞, ∞), where higher is more similar.
Best for: Pre-normalized vectors, recommendation systems.
Manhattan
Manhattan (L1) distance.
Sum of absolute differences across dimensions. Range: [0, ∞), where 0 means identical vectors.
Best for: Sparse vectors, grid-based navigation.
Implementations§
Source§impl DistanceMetric
impl DistanceMetric
Sourcepub fn formula(&self) -> &'static str
pub fn formula(&self) -> &'static str
Mathematical formula for this metric’s distance computation.
Sourcepub fn similarity(&self, a: &[f32], b: &[f32]) -> f32
pub fn similarity(&self, a: &[f32], b: &[f32]) -> f32
Compute the similarity score between two vectors.
Returns a score where higher is more similar for all metrics. For distance-based metrics (Euclidean, Manhattan), this returns a transformed score in [0, 1] range.
§Panics
Panics if vectors have different lengths.
Sourcepub fn distance(&self, a: &[f32], b: &[f32]) -> f32
pub fn distance(&self, a: &[f32], b: &[f32]) -> f32
Compute the raw distance between two vectors.
Returns a distance where lower means more similar. For similarity-based metrics (Cosine, DotProduct), this returns a transformed distance.
Sourcepub fn is_similarity_based(&self) -> bool
pub fn is_similarity_based(&self) -> bool
Returns true if this metric is similarity-based (higher = more similar).
Sourcepub fn is_distance_based(&self) -> bool
pub fn is_distance_based(&self) -> bool
Returns true if this metric is distance-based (lower = more similar).
Trait Implementations§
Source§impl Clone for DistanceMetric
impl Clone for DistanceMetric
impl Copy for DistanceMetric
Source§impl Debug for DistanceMetric
impl Debug for DistanceMetric
Source§impl Default for DistanceMetric
impl Default for DistanceMetric
Source§impl<'de> Deserialize<'de> for DistanceMetric
impl<'de> Deserialize<'de> for DistanceMetric
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for DistanceMetric
impl Display for DistanceMetric
impl Eq for DistanceMetric
Source§impl FromStr for DistanceMetric
impl FromStr for DistanceMetric
Source§impl Hash for DistanceMetric
impl Hash for DistanceMetric
Source§impl PartialEq for DistanceMetric
impl PartialEq for DistanceMetric
Source§impl Serialize for DistanceMetric
impl Serialize for DistanceMetric
impl StructuralPartialEq for DistanceMetric
Auto Trait Implementations§
impl Freeze for DistanceMetric
impl RefUnwindSafe for DistanceMetric
impl Send for DistanceMetric
impl Sync for DistanceMetric
impl Unpin for DistanceMetric
impl UnsafeUnpin for DistanceMetric
impl UnwindSafe for DistanceMetric
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
self to key and returns true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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