MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / search_with_metric

Method search_with_metric

nodedb-vector/src/flat.rs:74–114  ·  view source on GitHub ↗

Brute-force k-NN search with an explicit distance metric override. Overrides the `self.metric` configured at collection creation time.

(
        &self,
        query: &[f32],
        top_k: usize,
        metric: DistanceMetric,
    )

Source from the content-addressed store, hash-verified

72 /// Brute-force k-NN search with an explicit distance metric override.
73 /// Overrides the `self.metric` configured at collection creation time.
74 pub fn search_with_metric(
75 &self,
76 query: &[f32],
77 top_k: usize,
78 metric: DistanceMetric,
79 ) -> Vec<SearchResult> {
80 assert_eq!(query.len(), self.dim);
81 let n = self.len();
82 if n == 0 || top_k == 0 {
83 return Vec::new();
84 }
85
86 let mut candidates: Vec<SearchResult> = Vec::with_capacity(n.min(top_k * 2));
87 for i in 0..n {
88 if self.deleted[i] {
89 continue;
90 }
91 let start = i * self.dim;
92 let vec_slice = &self.data[start..start + self.dim];
93 let dist = distance(query, vec_slice, metric);
94 candidates.push(SearchResult {
95 id: i as u32,
96 distance: dist,
97 });
98 }
99
100 if candidates.len() > top_k {
101 candidates.select_nth_unstable_by(top_k, |a, b| {
102 a.distance
103 .partial_cmp(&b.distance)
104 .unwrap_or(std::cmp::Ordering::Equal)
105 });
106 candidates.truncate(top_k);
107 }
108 candidates.sort_by(|a, b| {
109 a.distance
110 .partial_cmp(&b.distance)
111 .unwrap_or(std::cmp::Ordering::Equal)
112 });
113 candidates
114 }
115
116 /// Brute-force k-NN search. Exact results — no approximation.
117 pub fn search(&self, query: &[f32], top_k: usize) -> Vec<SearchResult> {

Callers 1

execute_vector_searchMethod · 0.45

Calls 5

distanceFunction · 0.50
lenMethod · 0.45
pushMethod · 0.45
partial_cmpMethod · 0.45
truncateMethod · 0.45

Tested by

no test coverage detected