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
//! Unified scored result type for vector search and graph traversal.
//!
//! `ScoredResult` replaces scattered `(u64, f32)` tuple patterns across search
//! paths, providing a named, self-documenting type with bidirectional conversions.
use crate::sparse_index::types::ScoredDoc;
/// A search result pairing an item identifier with a relevance score.
///
/// Used as the canonical return type across vector search, sparse search,
/// and hybrid fusion pipelines.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScoredResult {
/// Unique identifier of the matched item.
pub id: u64,
/// Relevance score (interpretation depends on metric: higher may be better
/// for similarity, lower for distance).
pub score: f32,
}
impl ScoredResult {
/// Creates a new scored result.
#[must_use]
#[inline]
pub fn new(id: u64, score: f32) -> Self {
Self { id, score }
}
}
// --- Tuple conversions ---
impl From<(u64, f32)> for ScoredResult {
#[inline]
fn from((id, score): (u64, f32)) -> Self {
Self { id, score }
}
}
impl From<ScoredResult> for (u64, f32) {
#[inline]
fn from(sr: ScoredResult) -> Self {
(sr.id, sr.score)
}
}
// --- ScoredDoc conversions ---
impl From<ScoredDoc> for ScoredResult {
#[inline]
fn from(sd: ScoredDoc) -> Self {
Self {
id: sd.doc_id,
score: sd.score,
}
}
}
impl From<ScoredResult> for ScoredDoc {
#[inline]
fn from(sr: ScoredResult) -> Self {
Self {
doc_id: sr.id,
score: sr.score,
}
}
}
#[cfg(test)]
#[path = "scored_result_tests.rs"]
mod tests;