Skip to content

Commit cb93db6

Browse files
Add similarity search
1 parent 92e0c04 commit cb93db6

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/indexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct IndexPoint {
3333
}
3434

3535
impl Point {
36-
fn id(&self) -> &String {
36+
pub fn id(&self) -> &String {
3737
match self {
3838
Point::Stored { id, vec } => id,
3939
Point::Mem { vec } => panic!("You can not get the external id of a memory point"),

src/server.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bytes::Bytes;
33
use futures::StreamExt;
44
use futures::TryStreamExt;
55
use hnsw::Hnsw;
6+
use hyper::StatusCode;
67
use hyper::{
78
service::{make_service_fn, service_fn},
89
Body, Method, Request, Response, Server, Uri,
@@ -397,18 +398,31 @@ impl Service {
397398
let index_id = create_index_name(&domain, &commit);
398399
// if None, then return 404
399400
let hnsw = self.get_index(&index_id).await.unwrap();
400-
let qp;
401-
todo!();
402-
let res = search(&qp, count, &hnsw).unwrap();
403-
let ids: Vec<QueryResult> = res
404-
.iter()
405-
.map(|p| QueryResult {
406-
id: p.id().to_string(),
407-
distance: f32::from_bits(p.distance()),
408-
})
409-
.collect();
410-
let s = serde_json::to_string(&ids).unwrap();
411-
Ok(Response::builder().body(s.into()).unwrap())
401+
let elts = hnsw.layer_len(0);
402+
let mut qp = None;
403+
for i in 0..elts {
404+
if *hnsw.feature(i).id() == id {
405+
qp = Some(hnsw.feature(i))
406+
}
407+
}
408+
match qp {
409+
Some(qp) => {
410+
let res = search(qp, count, &hnsw).unwrap();
411+
let ids: Vec<QueryResult> = res
412+
.iter()
413+
.map(|p| QueryResult {
414+
id: p.id().to_string(),
415+
distance: f32::from_bits(p.distance()),
416+
})
417+
.collect();
418+
let s = serde_json::to_string(&ids).unwrap();
419+
Ok(Response::builder().body(s.into()).unwrap())
420+
}
421+
None => Ok(Response::builder()
422+
.status(StatusCode::NOT_FOUND)
423+
.body("id not found".into())
424+
.unwrap()),
425+
}
412426
}
413427
Ok(_) => todo!(),
414428
Err(_) => todo!(),

0 commit comments

Comments
 (0)