@@ -315,6 +315,8 @@ enum ResponseError {
315
315
StartIndexError ( #[ from] StartIndexError ) ,
316
316
#[ error( "{0:?}" ) ]
317
317
SearchError ( #[ from] SearchError ) ,
318
+ #[ error( "Missing id in index {0}" ) ]
319
+ IdMissing ( String ) ,
318
320
}
319
321
320
322
fn add_to_duplicates ( duplicates : & mut HashMap < usize , usize > , id1 : usize , id2 : usize ) {
@@ -526,7 +528,7 @@ impl Service {
526
528
previous,
527
529
} ) => {
528
530
let result = self . get_start_index ( req, domain, commit, previous) . await ;
529
- fun_name ( result)
531
+ string_response_or_error ( result)
530
532
}
531
533
Ok ( ResourceSpec :: AssignIndex {
532
534
domain,
@@ -569,45 +571,16 @@ impl Service {
569
571
let result = self
570
572
. get_duplicate_candidates ( domain, commit, threshold)
571
573
. await ;
572
- match result {
573
- Ok ( result) => todo ! ( ) ,
574
- Err ( e) => todo ! ( ) ,
575
- }
574
+ string_response_or_error ( result)
576
575
}
577
576
Ok ( ResourceSpec :: Similar {
578
577
domain,
579
578
commit,
580
579
count,
581
580
id,
582
581
} ) => {
583
- let index_id = create_index_name ( & domain, & commit) ;
584
- // if None, then return 404
585
- let hnsw = self . get_index ( & index_id) . await . unwrap ( ) ;
586
- let elts = hnsw. layer_len ( 0 ) ;
587
- let mut qp = None ;
588
- for i in 0 ..elts {
589
- if * hnsw. feature ( i) . id ( ) == id {
590
- qp = Some ( hnsw. feature ( i) )
591
- }
592
- }
593
- match qp {
594
- Some ( qp) => {
595
- let res = search ( qp, count, & hnsw) . unwrap ( ) ;
596
- let ids: Vec < QueryResult > = res
597
- . iter ( )
598
- . map ( |p| QueryResult {
599
- id : p. id ( ) . to_string ( ) ,
600
- distance : f32:: from_bits ( p. distance ( ) ) ,
601
- } )
602
- . collect ( ) ;
603
- let s = serde_json:: to_string ( & ids) . unwrap ( ) ;
604
- Ok ( Response :: builder ( ) . body ( s. into ( ) ) . unwrap ( ) )
605
- }
606
- None => Ok ( Response :: builder ( )
607
- . status ( StatusCode :: NOT_FOUND )
608
- . body ( "id not found" . into ( ) )
609
- . unwrap ( ) ) ,
610
- }
582
+ let result = self . get_similar_documents ( domain, commit, id, count) . await ;
583
+ string_response_or_error ( result)
611
584
}
612
585
Ok ( _) => todo ! ( ) ,
613
586
Err ( e) => Ok ( Response :: builder ( )
@@ -617,6 +590,40 @@ impl Service {
617
590
}
618
591
}
619
592
593
+ async fn get_similar_documents (
594
+ self : Arc < Self > ,
595
+ domain : String ,
596
+ commit : String ,
597
+ id : String ,
598
+ count : usize ,
599
+ ) -> Result < String , ResponseError > {
600
+ let index_id = create_index_name ( & domain, & commit) ;
601
+ // if None, then return 404
602
+ let hnsw = self . get_index ( & index_id) . await ?;
603
+ let elts = hnsw. layer_len ( 0 ) ;
604
+ let mut qp = None ;
605
+ for i in 0 ..elts {
606
+ if * hnsw. feature ( i) . id ( ) == id {
607
+ qp = Some ( hnsw. feature ( i) )
608
+ }
609
+ }
610
+ match qp {
611
+ Some ( qp) => {
612
+ let res = search ( qp, count, & hnsw) ?;
613
+ let ids: Vec < QueryResult > = res
614
+ . iter ( )
615
+ . map ( |p| QueryResult {
616
+ id : p. id ( ) . to_string ( ) ,
617
+ distance : f32:: from_bits ( p. distance ( ) ) ,
618
+ } )
619
+ . collect ( ) ;
620
+ let s = serde_json:: to_string ( & ids) ?;
621
+ Ok ( s)
622
+ }
623
+ None => Err ( ResponseError :: IdMissing ( id) ) ,
624
+ }
625
+ }
626
+
620
627
async fn get_duplicate_candidates (
621
628
self : Arc < Self > ,
622
629
domain : String ,
@@ -702,7 +709,9 @@ impl Service {
702
709
}
703
710
}
704
711
705
- fn fun_name ( result : Result < String , ResponseError > ) -> Result < Response < Body > , Infallible > {
712
+ fn string_response_or_error (
713
+ result : Result < String , ResponseError > ,
714
+ ) -> Result < Response < Body > , Infallible > {
706
715
match result {
707
716
Ok ( task_id) => Ok ( Response :: builder ( ) . body ( task_id. into ( ) ) . unwrap ( ) ) ,
708
717
Err ( e) => Ok ( Response :: builder ( )
0 commit comments