Skip to content

Commit

Permalink
MB-32183 - new Levenshtein automaton
Browse files Browse the repository at this point in the history
Adopting new Levenshtein automaton
  • Loading branch information
sreekanth-cb committed Dec 5, 2018
1 parent b08a857 commit 3454eca
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions index/scorch/snapshot_index.go
Expand Up @@ -27,9 +27,14 @@ import (
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/scorch/segment"
"github.com/couchbase/vellum/levenshtein"
"github.com/couchbase/vellum"
lev2 "github.com/couchbase/vellum/levenshtein2"
)

// re usable, threadsafe levenshtein builders
var lb1, lb2 *lev2.LevenshteinAutomatonBuilder
var lbErr1, lbErr2 error

type asynchSegmentResult struct {
dictItr segment.DictionaryIterator

Expand All @@ -46,6 +51,8 @@ var reflectStaticSizeIndexSnapshot int
func init() {
var is interface{} = IndexSnapshot{}
reflectStaticSizeIndexSnapshot = int(reflect.TypeOf(is).Size())
lb1, lbErr1 = lev2.NewLevenshteinAutomatonBuilder(1, true)
lb2, lbErr2 = lev2.NewLevenshteinAutomatonBuilder(2, true)
}

type IndexSnapshot struct {
Expand Down Expand Up @@ -194,9 +201,26 @@ func (i *IndexSnapshot) FieldDictRegexp(field string,
})
}

func (i *IndexSnapshot) getLevAutomaton(term string,
fuzziness uint8) (vellum.Automaton, error) {
if fuzziness == 1 {
if lb1 != nil {
return lb1.BuildDfa(term, fuzziness)
}
return nil, lbErr1

} else if fuzziness == 2 {
if lb2 != nil {
return lb2.BuildDfa(term, fuzziness)
}
return nil, lbErr2
}
return nil, nil
}

func (i *IndexSnapshot) FieldDictFuzzy(field string,
term string, fuzziness int, prefix string) (index.FieldDict, error) {
a, err := levenshtein.New(term, fuzziness)
a, err := i.getLevAutomaton(term, uint8(fuzziness))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3454eca

Please sign in to comment.