Skip to content

Commit

Permalink
update formula
Browse files Browse the repository at this point in the history
  • Loading branch information
metonymic-smokey committed Apr 19, 2024
1 parent b2384fc commit 2f45298
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions section_faiss_vector_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ func (v *vectorIndexOpaque) mergeAndWriteVectorIndexes(sbs []*SegmentBase,

nvecs := len(finalVecIDs)

// index type to be created after merge based on the number of vectors in
// indexData added into the index.
indexOptimizedFor := indexes[0].indexOptimizedFor

// index type to be created after merge based on the number of vectors
// in indexData added into the index and chosen optimization.
nlist := determineCentroids(nvecs)
indexDescription, indexClass := determineIndexToUse(nvecs, nlist)
indexDescription, indexClass := determineIndexToUse(nvecs, nlist, indexOptimizedFor)

// safe to assume that all the indexes are of the same config values, given
// that they are extracted from the field mapping info.
dims := vecIndexes[0].D()
metric := vecIndexes[0].MetricType()
indexOptimizedFor := indexes[0].indexOptimizedFor

// freeing the reconstructed indexes immediately - waiting till the end
// to do the same is not needed because the following operations don't need
Expand Down Expand Up @@ -459,15 +460,17 @@ const (

// Returns a description string for the index and quantizer type
// and an index type.
func determineIndexToUse(nvecs, nlist int) (string, int) {
switch {
case nvecs >= 10000:
return fmt.Sprintf("IVF%d,SQ8", nlist), IndexTypeIVF
case nvecs >= 1000:
return fmt.Sprintf("IVF%d,Flat", nlist), IndexTypeIVF
default:
func determineIndexToUse(nvecs, nlist int, indexOptimisedFor string) (string, int) {
if nvecs < 1000 {
return "IDMap2,Flat", IndexTypeFlat
}

// If optimised for latency, use Flat.
if indexOptimisedFor == index.IndexOptimizedForLatency {
return fmt.Sprintf("IVF%d,Flat", nlist), IndexTypeIVF
}

return fmt.Sprintf("IVF%d,SQ8", nlist), IndexTypeIVF
}

func (vo *vectorIndexOpaque) writeVectorIndexes(w *CountHashWriter) (offset uint64, err error) {
Expand All @@ -492,7 +495,8 @@ func (vo *vectorIndexOpaque) writeVectorIndexes(w *CountHashWriter) (offset uint

nvecs := len(ids)
nlist := determineCentroids(nvecs)
indexDescription, indexClass := determineIndexToUse(nvecs, nlist)
indexDescription, indexClass := determineIndexToUse(nvecs, nlist,
content.indexOptimizedFor)
faissIndex, err := faiss.IndexFactory(int(content.dim), indexDescription, metric)
if err != nil {
return 0, err
Expand Down

0 comments on commit 2f45298

Please sign in to comment.