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

Method search

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

Brute-force k-NN search. Exact results — no approximation.

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

Source from the content-addressed store, hash-verified

115
116 /// Brute-force k-NN search. Exact results — no approximation.
117 pub fn search(&self, query: &[f32], top_k: usize) -> Vec<SearchResult> {
118 assert_eq!(query.len(), self.dim);
119 let n = self.len();
120 if n == 0 || top_k == 0 {
121 return Vec::new();
122 }
123
124 let mut candidates: Vec<SearchResult> = Vec::with_capacity(n.min(top_k * 2));
125 for i in 0..n {
126 if self.deleted[i] {
127 continue;
128 }
129 let start = i * self.dim;
130 let vec_slice = &self.data[start..start + self.dim];
131 let dist = distance(query, vec_slice, self.metric);
132 candidates.push(SearchResult {
133 id: i as u32,
134 distance: dist,
135 });
136 }
137
138 if candidates.len() > top_k {
139 candidates.select_nth_unstable_by(top_k, |a, b| {
140 a.distance
141 .partial_cmp(&b.distance)
142 .unwrap_or(std::cmp::Ordering::Equal)
143 });
144 candidates.truncate(top_k);
145 }
146 candidates.sort_by(|a, b| {
147 a.distance
148 .partial_cmp(&b.distance)
149 .unwrap_or(std::cmp::Ordering::Equal)
150 });
151 candidates
152 }
153
154 /// Search with a pre-filter bitmap (byte-array format).
155 pub fn search_filtered(&self, query: &[f32], top_k: usize, bitmap: &[u8]) -> Vec<SearchResult> {

Callers 4

insert_and_searchFunction · 0.45
exact_resultsFunction · 0.45
empty_searchFunction · 0.45

Calls 5

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

Tested by 4

insert_and_searchFunction · 0.36
exact_resultsFunction · 0.36
empty_searchFunction · 0.36