Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/search/search.go
1560 views
1
package search
2
3
import (
4
"context"
5
"fmt"
6
7
"github.com/alist-org/alist/v3/internal/conf"
8
"github.com/alist-org/alist/v3/internal/errs"
9
"github.com/alist-org/alist/v3/internal/model"
10
"github.com/alist-org/alist/v3/internal/op"
11
"github.com/alist-org/alist/v3/internal/search/searcher"
12
log "github.com/sirupsen/logrus"
13
)
14
15
var instance searcher.Searcher = nil
16
17
// Init or reset index
18
func Init(mode string) error {
19
if instance != nil {
20
// unchanged, do nothing
21
if instance.Config().Name == mode {
22
return nil
23
}
24
err := instance.Release(context.Background())
25
if err != nil {
26
log.Errorf("release instance err: %+v", err)
27
}
28
instance = nil
29
}
30
if Running() {
31
return fmt.Errorf("index is running")
32
}
33
if mode == "none" {
34
log.Warnf("not enable search")
35
return nil
36
}
37
s, ok := searcher.NewMap[mode]
38
if !ok {
39
return fmt.Errorf("not support index: %s", mode)
40
}
41
i, err := s()
42
if err != nil {
43
log.Errorf("init searcher error: %+v", err)
44
} else {
45
instance = i
46
}
47
return err
48
}
49
50
func Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) {
51
return instance.Search(ctx, req)
52
}
53
54
func Index(ctx context.Context, parent string, obj model.Obj) error {
55
if instance == nil {
56
return errs.SearchNotAvailable
57
}
58
return instance.Index(ctx, model.SearchNode{
59
Parent: parent,
60
Name: obj.GetName(),
61
IsDir: obj.IsDir(),
62
Size: obj.GetSize(),
63
})
64
}
65
66
type ObjWithParent struct {
67
Parent string
68
model.Obj
69
}
70
71
func BatchIndex(ctx context.Context, objs []ObjWithParent) error {
72
if instance == nil {
73
return errs.SearchNotAvailable
74
}
75
if len(objs) == 0 {
76
return nil
77
}
78
var searchNodes []model.SearchNode
79
for i := range objs {
80
searchNodes = append(searchNodes, model.SearchNode{
81
Parent: objs[i].Parent,
82
Name: objs[i].GetName(),
83
IsDir: objs[i].IsDir(),
84
Size: objs[i].GetSize(),
85
})
86
}
87
return instance.BatchIndex(ctx, searchNodes)
88
}
89
90
func init() {
91
op.RegisterSettingItemHook(conf.SearchIndex, func(item *model.SettingItem) error {
92
log.Debugf("searcher init, mode: %s", item.Value)
93
return Init(item.Value)
94
})
95
}
96
97