Skip to content

Commit

Permalink
Fix: Index fails to search first inserted vector
Browse files Browse the repository at this point in the history
  • Loading branch information
miamia0 committed Feb 19, 2025
1 parent 1755c0a commit e7ebfff
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/hnsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,6 @@ impl<'b, T: Clone + Send + Sync, D: Distance<T> + Send + Sync> Hnsw<'b, T, D> {
// we will store positive distances in this one
let mut return_points = BinaryHeap::<Arc<PointWithOrder<T>>>::with_capacity(skiplist_size);
//
if self.layer_indexed_points.points_by_layer.read()[layer as usize].is_empty() {
// at the beginning we can have nothing in layer
trace!("search layer {:?}, empty layer", layer);
return return_points;
}
if entry_point.p_id.1 < 0 {
trace!("search layer negative point id : {:?}", entry_point.p_id);
return return_points;
Expand Down Expand Up @@ -1066,6 +1061,10 @@ impl<'b, T: Clone + Send + Sync, D: Distance<T> + Send + Sync> Hnsw<'b, T, D> {
let level = new_point.p_id.0;
let mut enter_point_copy = None;
let mut max_level_observed = 0;
if enter_point_copy.is_none() {
self.layer_indexed_points.check_entry_point(&new_point);
}

// entry point has been set in
{
// I open a read lock on an option
Expand All @@ -1076,15 +1075,10 @@ impl<'b, T: Clone + Send + Sync, D: Distance<T> + Send + Sync> Hnsw<'b, T, D> {
"Hnsw stored first point , direct return {:?} ",
new_point.p_id
);
return;
}
max_level_observed = enter_point_copy.as_ref().unwrap().p_id.0;
}
}
if enter_point_copy.is_none() {
self.layer_indexed_points.check_entry_point(&new_point);
return;
}
let mut dist_to_entry = self
.dist_f
.eval(data, enter_point_copy.as_ref().unwrap().data.get_v());
Expand Down Expand Up @@ -1703,7 +1697,17 @@ where
mod tests {

use super::*;
use anndists::dist;
use anndists::dist::{self, DistL1};

#[test]
fn test_insert() {
for _ in 0..10 {
let hnsw: Hnsw<f32, DistL1> = Hnsw::new(15, 100_000, 20, 500_000, DistL1 {});
hnsw.insert((&[1.0, 0.0, 0.0, 0.0], 0));
let result = hnsw.search(&[1.0, 0.0, 0.0, 0.0], 2, 10);
assert_eq!(result, vec![Neighbour::new(0, 0.0, PointId(0, 0))]);
}
}

#[test]

Expand Down

0 comments on commit e7ebfff

Please sign in to comment.