Skip to content

Commit

Permalink
fix(vector): fix index rebuild when options change
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil-goel committed Aug 28, 2024
1 parent bcf6ed8 commit c657b0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
44 changes: 38 additions & 6 deletions posting/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,14 +1218,22 @@ func (rb *IndexRebuild) needsTokIndexRebuild() *indexRebuildInfo {
prevFactoryNames := make(map[string]struct{})
prevFactories := make(map[string]*pb.VectorIndexSpec)
for _, t := range old.IndexSpecs {
prevFactoryNames[t.Name] = struct{}{}
prevFactories[t.Name] = t
spec, err := tok.GetFactoryCreateSpecFromSpec(t)
x.AssertTruef(err == nil, "Error while building index spec %s", err)
name := spec.Name()

prevFactoryNames[name] = struct{}{}
prevFactories[name] = t
}
currFactoryNames := make(map[string]struct{})
currFactories := make(map[string]*pb.VectorIndexSpec)
for _, t := range rb.CurrentSchema.IndexSpecs {
currFactoryNames[t.Name] = struct{}{}
currFactories[t.Name] = t
spec, err := tok.GetFactoryCreateSpecFromSpec(t)
x.AssertTruef(err == nil, "Error while building index spec %s", err)
name := spec.Name()

currFactoryNames[name] = struct{}{}
currFactories[name] = t
}

newFactoryNames, deletedFactoryNames := x.Diff(currFactoryNames, prevFactoryNames)
Expand Down Expand Up @@ -1532,13 +1540,37 @@ func (rb *IndexRebuild) needsVectorIndexEdgesRebuild() indexOp {
prevIndex := old.Directive == pb.SchemaUpdate_INDEX &&
old.ValueType == pb.Posting_VFLOAT

prevFactoryNames := make(map[string]struct{})
prevFactories := make(map[string]*pb.VectorIndexSpec)
for _, t := range old.IndexSpecs {
spec, err := tok.GetFactoryCreateSpecFromSpec(t)
x.AssertTruef(err == nil, "Error while building index spec %s", err)
name := spec.Name()

prevFactoryNames[name] = struct{}{}
prevFactories[name] = t
}

currFactoryNames := make(map[string]struct{})
currFactories := make(map[string]*pb.VectorIndexSpec)
for _, t := range rb.CurrentSchema.IndexSpecs {
spec, err := tok.GetFactoryCreateSpecFromSpec(t)
x.AssertTruef(err == nil, "Error while building index spec %s", err)
name := spec.Name()

currFactoryNames[name] = struct{}{}
currFactories[name] = t
}

newFactoryNames, deletedFactoryNames := x.Diff(currFactoryNames, prevFactoryNames)

// If the schema directive did not change, return indexNoop.
if currIndex == prevIndex {
if currIndex == prevIndex && len(newFactoryNames) == 0 && len(deletedFactoryNames) == 0 {
return indexNoop
}

// If the current schema requires an index, index should be rebuilt.
if currIndex {
if currIndex || len(newFactoryNames) != 0 {
return indexRebuild
}
// Otherwise, index should only be deleted.
Expand Down
4 changes: 4 additions & 0 deletions tok/hnsw/persistent_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package hnsw

import (
"fmt"
"sync"

c "github.com/dgraph-io/dgraph/v24/tok/constraints"
Expand Down Expand Up @@ -80,6 +81,9 @@ func (hf *persistentIndexFactory[T]) AllowedOptions() opt.AllowedOptions {
AddIntOption(EfConstructionOpt).
AddIntOption(EfSearchOpt)
getSimFunc := func(optValue string) (any, error) {
if optValue != Euclidian && optValue != Cosine && optValue != DotProd {
return nil, errors.New(fmt.Sprintf("Can't create a vector index for %s", optValue))
}
return GetSimType[T](optValue, hf.floatBits), nil
}

Expand Down

0 comments on commit c657b0d

Please sign in to comment.