1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Distance metrics for vector similarity calculations.
//!
//! # Performance
//!
//! All distance calculations use direct SIMD dispatch via `simd_native` module,
//! eliminating intermediate dispatch overhead for maximum performance:
//! - **Cosine**: Direct AVX-512/AVX2/NEON intrinsics
//! - **Euclidean**: Direct native intrinsics with 4-acc unrolling
//! - **Dot Product**: Direct FMA-optimized intrinsics
//! - **Hamming (binary)**: `DistanceMetric::calculate` uses the f32 variant
//! (0.5 threshold per component); the POPCNT-on-packed-u64 fast path
//! is a separate API consumed by the `RaBitQ` pipeline
//! - **Jaccard**: Set similarity with SIMD acceleration
use crate::simd_native;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
/// Canonical names of every [`DistanceMetric`] variant, in declaration order.
///
/// Single source of truth for the metric name set exported to downstream
/// crates and bindings (Python `velesdb.DISTANCE_METRICS`, the integrations
/// security guard). Each entry is the variant's
/// [`canonical_name`](DistanceMetric::canonical_name); a unit test asserts the
/// slice stays exhaustive so adding a variant without updating it fails CI.
pub const DISTANCE_METRIC_NAMES: &[&str] = &["cosine", "euclidean", "dot", "hamming", "jaccard"];
/// Canonical serde `type` tags of every [`Condition`](crate::filter::Condition)
/// variant, in declaration order.
///
/// Single source of truth for the filter condition-type vocabulary exported to
/// downstream crates and bindings (the integrations' filter conversion and the
/// MIT-side security guard) so they never re-derive the tag spelling as literals
/// and drift when a variant is added. A unit test in `crate::filter` asserts the
/// slice round-trips against the actual serde representation so adding a variant
/// without updating it fails CI.
pub const CONDITION_TYPE_NAMES: &[&str] = &[
"eq",
"neq",
"gt",
"gte",
"lt",
"lte",
"in",
"contains",
"is_null",
"is_not_null",
"and",
"or",
"not",
"like",
"ilike",
"array_contains",
"array_contains_any",
"array_contains_all",
"geo_distance",
"geo_bbox",
];
/// Distance metric for vector similarity calculations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DistanceMetric {
/// Cosine similarity (1 - `cosine_distance`).
/// Best for normalized vectors, commonly used with text embeddings.
Cosine,
/// Euclidean distance (L2 norm).
/// Best for spatial data and when magnitude matters.
Euclidean,
/// Dot product (inner product).
/// Best for maximum inner product search (MIPS).
DotProduct,
/// Hamming distance for binary vectors.
/// Counts the number of positions where bits differ.
/// Best for binary embeddings and locality-sensitive hashing.
Hamming,
/// Jaccard similarity for set-like vectors.
/// Measures intersection over union of non-zero elements.
/// Best for sparse vectors, tags, and set membership.
Jaccard,
}
impl DistanceMetric {
/// Returns the canonical metric name used by user-facing APIs.
#[must_use]
pub const fn canonical_name(self) -> &'static str {
match self {
Self::Cosine => "cosine",
Self::Euclidean => "euclidean",
Self::DotProduct => "dot",
Self::Hamming => "hamming",
Self::Jaccard => "jaccard",
}
}
/// Parses a metric name/alias into a [`DistanceMetric`].
///
/// Supported aliases:
/// - cosine
/// - euclidean, l2
/// - dot, dotproduct, inner, ip
/// - hamming
/// - jaccard
#[must_use]
pub fn parse_alias(value: &str) -> Option<Self> {
match value.trim().to_lowercase().as_str() {
"cosine" => Some(Self::Cosine),
"euclidean" | "l2" => Some(Self::Euclidean),
"dot" | "dotproduct" | "inner" | "ip" => Some(Self::DotProduct),
"hamming" => Some(Self::Hamming),
"jaccard" => Some(Self::Jaccard),
_ => None,
}
}
/// Calculates the distance between two vectors using the specified metric.
///
/// # Arguments
///
/// * `a` - First vector
/// * `b` - Second vector
///
/// # Returns
///
/// Distance value (lower is more similar for Euclidean, higher for Cosine/DotProduct).
///
/// # Panics
///
/// Panics if vectors have different dimensions.
///
/// # Performance
///
/// Uses SIMD-optimized implementations. Their measured latencies, each
/// with the run behind it, are in `docs/BENCHMARKS.md`.
#[must_use]
#[inline]
pub fn calculate(&self, a: &[f32], b: &[f32]) -> f32 {
match self {
Self::Cosine => simd_native::cosine_similarity_native(a, b),
Self::Euclidean => simd_native::euclidean_native(a, b),
Self::DotProduct => simd_native::dot_product_native(a, b),
Self::Hamming => simd_native::hamming_distance_native(a, b),
Self::Jaccard => simd_native::jaccard_similarity_native(a, b),
}
}
/// Returns whether higher values indicate more similarity.
#[must_use]
pub const fn higher_is_better(&self) -> bool {
match self {
Self::Cosine | Self::DotProduct | Self::Jaccard => true,
Self::Euclidean | Self::Hamming => false,
}
}
/// Returns whether an orthogonal change of basis leaves this metric
/// unchanged.
///
/// OPQ stores codes in a rotated space and rescoring rotates the query to
/// match. That is sound only for metrics built from inner products and
/// norms, which a rotation preserves: cosine, dot product, Euclidean.
/// Hamming and Jaccard are defined component by component on the original
/// axes — a rotated "binary" vector is no longer binary — so the same
/// trick computes a number that has no relation to the metric the user
/// asked for.
#[must_use]
pub const fn is_rotation_invariant(&self) -> bool {
match self {
Self::Cosine | Self::Euclidean | Self::DotProduct => true,
Self::Hamming | Self::Jaccard => false,
}
}
/// Returns this metric's score polarity for fusion
/// ([`ScoreDirection`](crate::fusion::ScoreDirection)).
///
/// Derived from [`Self::higher_is_better`] so the two can never drift.
#[must_use]
pub const fn score_direction(&self) -> crate::fusion::ScoreDirection {
if self.higher_is_better() {
crate::fusion::ScoreDirection::HigherIsBetter
} else {
crate::fusion::ScoreDirection::LowerIsBetter
}
}
/// The closed range this metric's user-visible scores occupy, or `None`
/// when the metric is unbounded.
///
/// This is the one definition of the score contract. A caller cannot tell
/// which path produced its results — `HnswIndex` reaches a score through
/// brute force, through the exact rerank, or through the graph's
/// `transform_score`, chosen by corpus size, quality and `k` — so those
/// paths must not each carry their own table. They derive from this one.
///
/// - **Cosine** is `[-1, 1]`. An anti-correlated pair has a genuinely
/// negative similarity; flooring it at zero does not merely lose the
/// sign, it ties every anti-correlated match at one value and destroys
/// the ordering among them.
/// - **Jaccard** is `[0, 1]`: `intersection / union` over non-negative
/// weights cannot go below zero.
/// - **Euclidean**, **Hamming** and **`DotProduct`** are unbounded — a
/// distance grows with the data, and an inner product is not normalized.
#[must_use]
pub const fn score_range(&self) -> Option<(f32, f32)> {
match self {
Self::Cosine => Some((-1.0, 1.0)),
Self::Jaccard => Some((0.0, 1.0)),
Self::Euclidean | Self::Hamming | Self::DotProduct => None,
}
}
/// Clamps `score` into [`Self::score_range`], leaving unbounded metrics
/// untouched.
///
/// Absorbs floating-point drift at the boundary without moving a value
/// that is genuinely inside the range.
#[must_use]
pub fn clamp_score(&self, score: f32) -> f32 {
match self.score_range() {
Some((low, high)) => score.clamp(low, high),
None => score,
}
}
/// Sorts search results by distance/similarity according to the metric.
///
/// - **Similarity metrics** (`Cosine`, `DotProduct`, `Jaccard`): sorts descending (higher = better)
/// - **Distance metrics** (`Euclidean`, `Hamming`): sorts ascending (lower = better)
///
/// Generic over the id type so both external ids (`u64`) and internal
/// HNSW node ids (`usize`) can be sorted with the same metric semantics.
///
/// # Example
///
/// ```rust,ignore
/// let mut results = vec![(1, 0.9), (2, 0.7), (3, 0.8)];
/// DistanceMetric::Cosine.sort_results(&mut results);
/// assert_eq!(results[0].0, 1); // Highest similarity first
/// ```
pub fn sort_results<Id>(&self, results: &mut [(Id, f32)]) {
if self.higher_is_better() {
// Similarity metrics: descending order (higher = better)
results.sort_unstable_by(|a, b| b.1.total_cmp(&a.1));
} else {
// Distance metrics: ascending order (lower = better)
results.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
}
}
/// Sorts scored results by the distance metric semantics.
///
/// Same as [`sort_results`](Self::sort_results) but operates on
/// [`ScoredResult`](crate::scored_result::ScoredResult) slices.
pub fn sort_scored_results(&self, results: &mut [crate::scored_result::ScoredResult]) {
if self.higher_is_better() {
results.sort_unstable_by(|a, b| b.score.total_cmp(&a.score));
} else {
results.sort_unstable_by(|a, b| a.score.total_cmp(&b.score));
}
}
/// Top-k twin of [`Self::sort_scored_results`]: O(n + k log k) partial
/// select + sort instead of a full O(n log n) sort, for callers that
/// truncate to `k` anyway — the bitmap brute-force scan hands this the
/// whole allowed set, which can be a large fraction of the collection.
///
/// Gated with the `index` module its helper lives in (its callers are
/// the persistence-gated HNSW paths).
#[cfg(feature = "persistence")]
pub(crate) fn top_k_scored_results(
self,
results: &mut Vec<crate::scored_result::ScoredResult>,
k: usize,
) {
if self.higher_is_better() {
crate::index::top_k_partial_sort(results, k, |a, b| b.score.total_cmp(&a.score));
} else {
crate::index::top_k_partial_sort(results, k, |a, b| a.score.total_cmp(&b.score));
}
}
}
impl FromStr for DistanceMetric {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse_alias(s)
.ok_or("Unknown metric. Use: cosine, euclidean, l2, dot, dotproduct, inner, ip, hamming, jaccard")
}
}
#[cfg(test)]
#[path = "distance_metric_names_tests.rs"]
mod distance_metric_names_tests;