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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! HNSW vector searcher for approximate search.
use std::sync::Arc;
use crate::error::Result;
use crate::vector::core::vector::Vector;
use crate::vector::index::hnsw::graph::HnswGraph;
use crate::vector::index::hnsw::reader::HnswIndexReader;
use crate::vector::reader::VectorIndexReader;
use crate::vector::search::searcher::VectorIndexSearcher;
use crate::vector::search::searcher::{
VectorIndexQuery, VectorIndexQueryResult, VectorIndexQueryResults,
};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashSet};
/// HNSW vector searcher that performs approximate nearest neighbor search.
#[derive(Debug)]
pub struct HnswSearcher {
index_reader: Arc<dyn VectorIndexReader>,
ef_search: usize,
}
impl HnswSearcher {
/// Create a new HNSW searcher.
pub fn new(index_reader: Arc<dyn VectorIndexReader>) -> Result<Self> {
// Default ef_search value
let ef_search = 50;
Ok(Self {
index_reader,
ef_search,
})
}
/// Set the search parameter ef.
pub fn set_ef_search(&mut self, ef_search: usize) {
self.ef_search = ef_search;
}
}
impl VectorIndexSearcher for HnswSearcher {
fn search(&self, request: &VectorIndexQuery) -> Result<VectorIndexQueryResults> {
use std::time::Instant;
let start = Instant::now();
// correct approach: usage of downcast_ref to check if we can use graph search
if let Some(reader) = self.index_reader.as_any().downcast_ref::<HnswIndexReader>()
&& let Some(graph) = &reader.graph
&& let Some(ref field_name) = request.field_name
{
// Perform Graph Search
let mut results = self.search_graph(reader, graph, request, field_name)?;
results.search_time_ms = start.elapsed().as_secs_f64() * 1000.0;
return Ok(results);
}
// Fallback to Linear Scan (brute-force over all vectors)
let mut results = VectorIndexQueryResults::new();
let mut vector_ids = self.index_reader.vector_ids()?;
// Filter by field_name if specified
if let Some(ref field_name) = request.field_name {
vector_ids.retain(|(_, fname)| fname == field_name);
}
results.candidates_examined = vector_ids.len();
let mut candidates: Vec<(u64, String, f32, f32, Vector)> =
Vec::with_capacity(vector_ids.len());
for (doc_id, field_name) in vector_ids.iter() {
if let Ok(Some(vector)) = self.index_reader.get_vector(*doc_id, field_name) {
let similarity = self
.index_reader
.distance_metric()
.similarity(&request.query.data, &vector.data)?;
let distance = self
.index_reader
.distance_metric()
.distance(&request.query.data, &vector.data)?;
candidates.push((*doc_id, field_name.clone(), similarity, distance, vector));
}
}
candidates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
let top_k = request.params.top_k.min(candidates.len());
for (doc_id, field_name, similarity, distance, vector) in candidates.into_iter().take(top_k)
{
// Apply minimum similarity threshold
if similarity < request.params.min_similarity {
break;
}
let vector_output = if request.params.include_vectors {
Some(vector)
} else {
None
};
results
.results
.push(crate::vector::search::searcher::VectorIndexQueryResult {
doc_id,
field_name,
similarity,
distance,
vector: vector_output,
});
}
results.search_time_ms = start.elapsed().as_secs_f64() * 1000.0;
Ok(results)
}
fn count(&self, request: VectorIndexQuery) -> Result<u64> {
// Get all vector IDs with field names
let vector_ids = self.index_reader.vector_ids()?;
// Filter by field_name if specified
if let Some(ref field_name) = request.field_name {
Ok(vector_ids.iter().filter(|(_, f)| f == field_name).count() as u64)
} else {
Ok(vector_ids.len() as u64)
}
}
}
#[derive(Debug, Clone, PartialEq)]
struct Candidate {
id: u64,
distance: f32,
}
impl Eq for Candidate {}
impl Ord for Candidate {
fn cmp(&self, other: &Self) -> Ordering {
// Min-heap: smaller distance > larger distance for Visitor (nearest first)
// But for Result (Found), we might want Max-heap (furthest first) to keep ef smallest.
// HNSW logic typically uses Min-heap for "candidates to visit" and Max-heap for "dynamic list of found nearest"
// Here we define one Candidate struct. Let's assume standard PartialOrd (smaller < larger).
// Then BinaryHeap is MaxHeap (largest at top).
// This impl makes BinaryHeap a MIN-HEAP (smallest distance at top)
other
.distance
.partial_cmp(&self.distance)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for Candidate {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, PartialEq)]
struct ResultCandidate {
id: u64,
distance: f32,
}
impl Eq for ResultCandidate {}
impl Ord for ResultCandidate {
fn cmp(&self, other: &Self) -> Ordering {
// Max-heap: larger distance at top (to remove worst)
self.distance
.partial_cmp(&other.distance)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for ResultCandidate {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl HnswSearcher {
fn search_graph(
&self,
reader: &HnswIndexReader,
graph: &HnswGraph,
request: &VectorIndexQuery,
field_name: &str,
) -> Result<VectorIndexQueryResults> {
let entry_point = match graph.entry_point {
Some(ep) => ep,
None => return Ok(VectorIndexQueryResults::new()),
};
let query = &request.query;
let ef_search = self.ef_search;
// 1. Start from entry point at max_level
let mut curr_obj = entry_point;
// Note: Assuming entry_point is in field_name. If not, we might fail to get vector.
// If doc_id corresponds to field_name, we get vector.
// Since HNSW here is single-graph for mixed IDs (potentially), we must hope entry point is valid for calc_dist with this field?
// Ref discussion: assuming HnswIndex is single-field.
let mut dist = self.calc_dist(reader, query, curr_obj, field_name)?;
// 2. Greedy descent
for lc in (1..=graph.max_level).rev() {
let mut changed = true;
while changed {
changed = false;
if let Some(neighbors) = graph.get_neighbors(curr_obj, lc) {
for &neighbor_id in neighbors {
let d = self.calc_dist(reader, query, neighbor_id, field_name)?;
if d < dist {
dist = d;
curr_obj = neighbor_id;
changed = true;
}
}
}
}
}
// 3. Search at layer 0 with ef_search
let mut candidates = BinaryHeap::new(); // Min-heap (nearest first)
let mut found = BinaryHeap::new(); // Max-heap (furthest first)
candidates.push(Candidate {
id: curr_obj,
distance: dist,
});
found.push(ResultCandidate {
id: curr_obj,
distance: dist,
});
let mut visited = HashSet::new();
visited.insert(curr_obj);
while let Some(curr) = candidates.pop() {
if let Some(furthest) = found.peek()
&& curr.distance > furthest.distance
&& found.len() >= ef_search
{
break;
}
if let Some(neighbors) = graph.get_neighbors(curr.id, 0) {
for &neighbor_id in neighbors {
if visited.contains(&neighbor_id) {
continue;
}
visited.insert(neighbor_id);
let d = self.calc_dist(reader, query, neighbor_id, field_name)?;
let furthest_dist = found.peek().map(|c| c.distance).unwrap_or(f32::MAX);
if d < furthest_dist || found.len() < ef_search {
candidates.push(Candidate {
id: neighbor_id,
distance: d,
});
found.push(ResultCandidate {
id: neighbor_id,
distance: d,
});
if found.len() > ef_search {
found.pop();
}
}
}
}
}
// Convert found heaps to results
let mut final_results = Vec::new();
for c in found {
if let Ok(Some(vector)) = reader.get_vector(c.id, field_name) {
// Recalculate similarity? Or deduce from distance?
// DistanceMetric::similarity is not strictly inverse of distance for all metrics,
// but Reader knows metric.
let similarity = reader
.distance_metric()
.similarity(&query.data, &vector.data)?;
// Apply min_score
if similarity < request.params.min_similarity {
continue;
}
final_results.push(VectorIndexQueryResult {
doc_id: c.id,
field_name: field_name.to_string(),
similarity,
distance: c.distance,
vector: if request.params.include_vectors {
Some(vector)
} else {
None
},
});
}
}
// Sort results (similarity descending)
final_results.sort_by(|a, b| {
b.similarity
.partial_cmp(&a.similarity)
.unwrap_or(Ordering::Equal)
});
// Top K
let top_k = request.params.top_k.min(final_results.len());
final_results.truncate(top_k);
Ok(VectorIndexQueryResults {
results: final_results,
candidates_examined: visited.len(),
search_time_ms: 0.0, // Set by caller
query_metadata: std::collections::HashMap::new(),
})
}
fn calc_dist(
&self,
reader: &HnswIndexReader,
query: &Vector,
doc_id: u64,
field_name: &str,
) -> Result<f32> {
// Optimization: HnswIndexReader *could* support getting raw bytes or avoiding clone,
// but get_vector returns Option<Vector>.
if let Some(target) = reader.get_vector(doc_id, field_name)? {
reader.distance_metric().distance(&query.data, &target.data)
} else {
// Vector not found in this field?
// Should return max distance or error?
// Since graph contains doc_id, it should exist.
// But if mixed fields, it might not exist in *this* field.
Ok(f32::MAX)
}
}
}