/
indexer.go
45 lines (36 loc) · 912 Bytes
/
indexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package suggest
import (
"fmt"
"github.com/suggest-go/suggest/pkg/analysis"
"github.com/suggest-go/suggest/pkg/dictionary"
"github.com/suggest-go/suggest/pkg/index"
"github.com/suggest-go/suggest/pkg/store"
)
// Index builds a search index by using the given config and the dictionary
// and persists it the directory
func Index(
directory store.Directory,
dict dictionary.Dictionary,
config index.WriterConfig,
tokenizer analysis.Tokenizer,
) error {
encoder, err := index.NewEncoder()
if err != nil {
return fmt.Errorf("failed to create Encoder: %w", err)
}
indexWriter := index.NewIndexWriter(
directory,
config,
encoder,
)
err = dict.Iterate(func(key dictionary.Key, value dictionary.Value) error {
return indexWriter.AddDocument(key, tokenizer.Tokenize(value))
})
if err != nil {
return err
}
if err = indexWriter.Commit(); err != nil {
return err
}
return nil
}