reflex/cache/l2/
types.rs

1use crate::vectordb::rescoring::ScoredCandidate;
2use half::f16;
3
4#[derive(Debug, Clone)]
5/// Result of an L2 semantic lookup.
6pub struct L2LookupResult {
7    pub(crate) query_embedding: Vec<f16>,
8    pub(crate) candidates: Vec<ScoredCandidate>,
9    pub(crate) tenant_id: u64,
10    pub(crate) bq_candidates_count: usize,
11}
12
13impl L2LookupResult {
14    /// Creates a new lookup result.
15    pub fn new(
16        query_embedding: Vec<f16>,
17        candidates: Vec<ScoredCandidate>,
18        tenant_id: u64,
19        bq_candidates_count: usize,
20    ) -> Self {
21        Self {
22            query_embedding,
23            candidates,
24            tenant_id,
25            bq_candidates_count,
26        }
27    }
28
29    /// Returns the query embedding (f16).
30    pub fn query_embedding(&self) -> &[f16] {
31        &self.query_embedding
32    }
33
34    /// Returns scored candidates (sorted best-first).
35    pub fn candidates(&self) -> &[ScoredCandidate] {
36        &self.candidates
37    }
38
39    /// Consumes the result and returns the candidate vector.
40    pub fn into_candidates(self) -> Vec<ScoredCandidate> {
41        self.candidates
42    }
43
44    /// Returns the tenant id used for the search.
45    pub fn tenant_id(&self) -> u64 {
46        self.tenant_id
47    }
48
49    /// Returns the number of candidates returned by the BQ stage.
50    pub fn bq_candidates_count(&self) -> usize {
51        self.bq_candidates_count
52    }
53
54    /// Returns `true` if any candidates are present.
55    pub fn has_candidates(&self) -> bool {
56        !self.candidates.is_empty()
57    }
58
59    /// Returns the best candidate (if any).
60    pub fn best_candidate(&self) -> Option<&ScoredCandidate> {
61        self.candidates.first()
62    }
63}