@@ -474,7 +474,7 @@ func (vi *Index) Search(
474
474
searchSet * SearchSet ,
475
475
options SearchOptions ,
476
476
) error {
477
- vi .setupContext (idxCtx , treeKey , vec , options , LeafLevel )
477
+ vi .setupContext (idxCtx , treeKey , options , LeafLevel , vec , false /* transformed */ )
478
478
return vi .searchHelper (ctx , idxCtx , searchSet )
479
479
}
480
480
@@ -595,11 +595,11 @@ func (vi *Index) ForceMerge(
595
595
func (vi * Index ) setupInsertContext (idxCtx * Context , treeKey TreeKey , vec vector.T ) {
596
596
// Perform the search using quantized vectors rather than full vectors (i.e.
597
597
// skip reranking).
598
- vi .setupContext (idxCtx , treeKey , vec , SearchOptions {
598
+ vi .setupContext (idxCtx , treeKey , SearchOptions {
599
599
BaseBeamSize : vi .options .BaseBeamSize ,
600
600
SkipRerank : true ,
601
601
UpdateStats : ! vi .options .DisableAdaptiveSearch ,
602
- }, SecondLevel )
602
+ }, SecondLevel , vec , false /* transformed */ )
603
603
idxCtx .forInsert = true
604
604
}
605
605
@@ -608,27 +608,39 @@ func (vi *Index) setupDeleteContext(idxCtx *Context, treeKey TreeKey, vec vector
608
608
// Perform the search using quantized vectors rather than full vectors (i.e.
609
609
// skip reranking). Use a larger beam size to make it more likely that we'll
610
610
// find the vector to delete.
611
- vi .setupContext (idxCtx , treeKey , vec , SearchOptions {
611
+ vi .setupContext (idxCtx , treeKey , SearchOptions {
612
612
BaseBeamSize : vi .options .BaseBeamSize * 2 ,
613
613
SkipRerank : true ,
614
614
UpdateStats : ! vi .options .DisableAdaptiveSearch ,
615
- }, LeafLevel )
615
+ }, LeafLevel , vec , false /* transformed */ )
616
616
idxCtx .forDelete = true
617
617
}
618
618
619
- // setupContext sets up the given context as an operation is beginning.
619
+ // setupContext sets up the given context as an operation is beginning. If
620
+ // "randomized" is false, then the given vector is expected to be an original,
621
+ // unrandomized vector that was provided by the user. If "transformed" is true,
622
+ // then the vector is expected to already be randomized and normalized.
620
623
func (vi * Index ) setupContext (
621
- idxCtx * Context , treeKey TreeKey , vec vector.T , options SearchOptions , level Level ,
624
+ idxCtx * Context ,
625
+ treeKey TreeKey ,
626
+ options SearchOptions ,
627
+ level Level ,
628
+ vec vector.T ,
629
+ transformed bool ,
622
630
) {
623
631
idxCtx .treeKey = treeKey
624
632
idxCtx .level = level
625
- idxCtx .query .Init (vi .quantizer .GetDistanceMetric (), vec , & vi .rot )
626
633
idxCtx .forInsert = false
627
634
idxCtx .forDelete = false
628
635
idxCtx .options = options
629
636
if idxCtx .options .BaseBeamSize == 0 {
630
637
idxCtx .options .BaseBeamSize = vi .options .BaseBeamSize
631
638
}
639
+ if transformed {
640
+ idxCtx .query .InitTransformed (vi .quantizer .GetDistanceMetric (), vec , & vi .rot )
641
+ } else {
642
+ idxCtx .query .InitOriginal (vi .quantizer .GetDistanceMetric (), vec , & vi .rot )
643
+ }
632
644
}
633
645
634
646
// updateFunc is called by searchForUpdateHelper when it has a candidate
@@ -920,6 +932,25 @@ func (vi *Index) findExactDistances(
920
932
return candidates , nil
921
933
}
922
934
935
+ var err error
936
+ candidates , err = vi .getFullVectors (ctx , idxCtx , candidates )
937
+ if err != nil {
938
+ return nil , errors .Wrapf (err , "getting full vectors to find exact distances" )
939
+ }
940
+
941
+ // Compute exact distance between query vector and the data vectors.
942
+ idxCtx .query .ComputeExactDistances (idxCtx .level , candidates )
943
+
944
+ return candidates , nil
945
+ }
946
+
947
+ // getFullVectors fetches the full-size vectors for the given search candidates.
948
+ // These can be either leaf vectors fetched from the primary index or interior
949
+ // partition centroids fetched from the index. Fixups are enqueued for any
950
+ // vectors found to be "dangling".
951
+ func (vi * Index ) getFullVectors (
952
+ ctx context.Context , idxCtx * Context , candidates []SearchResult ,
953
+ ) ([]SearchResult , error ) {
923
954
// Prepare vector references.
924
955
idxCtx .tempVectorsWithKeys = utils .EnsureSliceLen (idxCtx .tempVectorsWithKeys , len (candidates ))
925
956
for i := range candidates {
@@ -956,9 +987,6 @@ func (vi *Index) findExactDistances(
956
987
}
957
988
}
958
989
959
- // Compute exact distance between query vector and the data vectors.
960
- idxCtx .query .ComputeExactDistances (idxCtx .level , candidates )
961
-
962
990
return candidates , nil
963
991
}
964
992
0 commit comments