-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.go
68 lines (60 loc) · 1.32 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"os"
"github.com/blevesearch/bleve/v2"
log "github.com/sirupsen/logrus"
)
func unIndex(uid string) error {
if err := idx.Delete(uid); err != nil {
log.Errorf("UnIndexed uid=%s success=false", uid)
return err
}
log.Infof("UnIndexed uid=%s success=true", uid)
return nil
}
func getIndex(path string) (bleve.Index, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
mapping := bleve.NewIndexMapping()
idx, err := bleve.New(path, mapping)
if err != nil {
return idx, err
}
idx.Close()
}
return bleve.Open(path)
}
func indexNote(note Note) error {
err := idx.Index(note.UID, note)
if err != nil {
log.Errorf("Indexed uid=%s success=false", note.UID)
return err
}
log.Infof("Indexed uid=%s success=true", note.UID)
return err
}
func searchIndex(q string) ([]string, error) {
query := bleve.NewQueryStringQuery(q)
in := bleve.NewSearchRequest(query)
out, err := idx.Search(in)
if err != nil {
return nil, err
}
uids := []string{}
for _, hit := range out.Hits {
uids = append(uids, string(hit.ID))
}
return uids, nil
}
func reIndex(b Backend) (int, error) {
var total int
for i, note := range b.list() {
content, _ := getContentByUID(b, note.UID, "")
note.Content = content
err := indexNote(note)
if err != nil {
return i, err
}
total++
}
return total, nil
}