feat: multiple search indexes (#2514)
* refactor: abstract search interface * wip: ~ * fix cycle import * objs update hook * wip: ~ * Delete search/none * auto update index while cache changed * db searcher TODO: bleve init issue cannot open index, metadata missing * fix size type why float64?? * fix typo * fix nil pointer using * api adapt ui * bleve: fix clear & change struct
This commit is contained in:
54
internal/search/search.go
Normal file
54
internal/search/search.go
Normal file
@ -0,0 +1,54 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/search/searcher"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var instance searcher.Searcher = nil
|
||||
|
||||
// Init or reset index
|
||||
func Init(mode string) error {
|
||||
if instance != nil {
|
||||
err := instance.Release(context.Background())
|
||||
if err != nil {
|
||||
log.Errorf("release instance err: %+v", err)
|
||||
}
|
||||
instance = nil
|
||||
}
|
||||
if Running {
|
||||
return fmt.Errorf("index is running")
|
||||
}
|
||||
if mode == "none" {
|
||||
log.Warnf("not enable search")
|
||||
return nil
|
||||
}
|
||||
s, ok := searcher.NewMap[mode]
|
||||
if !ok {
|
||||
return fmt.Errorf("not support index: %s", mode)
|
||||
}
|
||||
i, err := s()
|
||||
if err != nil {
|
||||
log.Errorf("init searcher error: %+v", err)
|
||||
} else {
|
||||
instance = i
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) {
|
||||
return instance.Search(ctx, req)
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterSettingItemHook(conf.SearchIndex, func(item *model.SettingItem) error {
|
||||
log.Debugf("searcher init, mode: %s", item.Value)
|
||||
return Init(item.Value)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user