Repository navigation
Expand file tree
/
Copy pathengine.go
More file actions
90 lines (76 loc) · 2.2 KB
/
Copy pathengine.go
File metadata and controls
90 lines (76 loc) · 2.2 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package tinysearch
import (
"database/sql"
"fmt"
"io"
"os"
"path/filepath"
)
// 検索エンジン
type Engine struct {
tokenizer *Tokenizer // トークンを分割する
indexer *Indexer // インデックスを作成する
documentStore *DocumentStore // ドキュメントを管理する
indexDir string // インデックスファイルを保存するディレクトリ
}
// 検索エンジンを作成する処理
func NewSearchEngine(db *sql.DB) *Engine {
tokenizer := NewTokenizer()
indexer := NewIndexer(tokenizer)
documentStore := NewDocumentStore(db)
path, ok := os.LookupEnv("INDEX_DIR_PATH")
if !ok {
current, _ := os.Getwd()
path = filepath.Join(current, "_index_data")
}
return &Engine{
tokenizer: tokenizer,
indexer: indexer,
documentStore: documentStore,
indexDir: path,
}
}
// インデックスにドキュメントを追加する
func (e *Engine) AddDocument(title string, reader io.Reader) error {
id, err := e.documentStore.save(title) // ❶ タイトルを保存しドキュメントIDを発行する
if err != nil {
return err
}
e.indexer.update(id, reader) // ❷ インデックスを更新する
return nil
}
// インデックスをファイルに書き出す
func (e *Engine) Flush() error {
writer := NewIndexWriter(e.indexDir)
return writer.Flush(e.indexer.index)
}
// 検索を実行する
func (e *Engine) Search(query string, k int) ([]*SearchResult, error) {
// クエリをトークンに分割
terms := e.tokenizer.TextToWordSequence(query)
// 検索を実行
docs := NewSearcher(e.indexDir).SearchTopK(terms, k)
// タイトルを取得
results := make([]*SearchResult, 0, k)
for _, result := range docs.scoreDocs {
title, err := e.documentStore.fetchTitle(result.docID)
if err != nil {
return nil, err
}
results = append(results, &SearchResult{
result.docID, result.score, title,
})
}
return results, nil
}
// 検索結果を格納する構造体
type SearchResult struct {
DocID DocumentID
Score float64
Title string
}
// String print SearchTopK result info
func (r *SearchResult) String() string {
return fmt.Sprintf("{DocID: %v, Score: %v, Title: %v}",
r.DocID, r.Score, r.Title)
}