diff --git a/internal/search/bleve/search.go b/internal/search/bleve/search.go index d629c7a1..083a644f 100644 --- a/internal/search/bleve/search.go +++ b/internal/search/bleve/search.go @@ -45,13 +45,8 @@ func (b *Bleve) Search(ctx context.Context, req model.SearchReq) ([]model.Search return res, int64(len(res)), nil } -func (b *Bleve) Index(ctx context.Context, parent string, obj model.Obj) error { - return b.BIndex.Index(uuid.NewString(), model.SearchNode{ - Parent: parent, - Name: obj.GetName(), - IsDir: obj.IsDir(), - Size: obj.GetSize(), - }) +func (b *Bleve) Index(ctx context.Context, node model.SearchNode) error { + return b.BIndex.Index(uuid.NewString(), node) } func (b *Bleve) Get(ctx context.Context, parent string) ([]model.SearchNode, error) { diff --git a/internal/search/build.go b/internal/search/build.go index 59bdfa42..aba92865 100644 --- a/internal/search/build.go +++ b/internal/search/build.go @@ -63,7 +63,7 @@ func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth if indexPath == "/" { return nil } - err = instance.Index(ctx, path.Dir(indexPath), info) + err = Index(ctx, path.Dir(indexPath), info) if err != nil { return err } else { diff --git a/internal/search/db/search.go b/internal/search/db/search.go index 92bb1645..a09ee6ca 100644 --- a/internal/search/db/search.go +++ b/internal/search/db/search.go @@ -18,13 +18,8 @@ func (D DB) Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode return db.SearchNode(req) } -func (D DB) Index(ctx context.Context, parent string, obj model.Obj) error { - return db.CreateSearchNode(&model.SearchNode{ - Parent: parent, - Name: obj.GetName(), - IsDir: obj.IsDir(), - Size: obj.GetSize(), - }) +func (D DB) Index(ctx context.Context, node model.SearchNode) error { + return db.CreateSearchNode(&node) } func (D DB) Get(ctx context.Context, parent string) ([]model.SearchNode, error) { diff --git a/internal/search/search.go b/internal/search/search.go index 1c836ebe..37148577 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -6,8 +6,10 @@ import ( "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/db" + "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/search/searcher" + "github.com/alist-org/alist/v3/pkg/utils" log "github.com/sirupsen/logrus" ) @@ -46,6 +48,18 @@ func Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64 return instance.Search(ctx, req) } +func Index(ctx context.Context, parent string, obj model.Obj) error { + if instance == nil { + return errs.SearchNotAvailable + } + return instance.Index(ctx, model.SearchNode{ + Parent: parent, + Name: utils.MappingName(obj.GetName(), conf.FilenameCharMap), + IsDir: obj.IsDir(), + Size: obj.GetSize(), + }) +} + func init() { db.RegisterSettingItemHook(conf.SearchIndex, func(item *model.SettingItem) error { log.Debugf("searcher init, mode: %s", item.Value) diff --git a/internal/search/searcher/searcher.go b/internal/search/searcher/searcher.go index 77378f32..96fa6a36 100644 --- a/internal/search/searcher/searcher.go +++ b/internal/search/searcher/searcher.go @@ -17,7 +17,7 @@ type Searcher interface { // Search specific keywords in specific path Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) // Index obj with parent - Index(ctx context.Context, parent string, obj model.Obj) error + Index(ctx context.Context, node model.SearchNode) error // Get by parent Get(ctx context.Context, parent string) ([]model.SearchNode, error) // Del with prefix diff --git a/internal/search/update.go b/internal/search/update.go index 2da44283..bec8e347 100644 --- a/internal/search/update.go +++ b/internal/search/update.go @@ -51,7 +51,7 @@ func Update(parent string, objs []model.Obj) { } for i := range objs { if toAdd.Contains(objs[i].GetName()) { - err = instance.Index(ctx, parent, objs[i]) + err = Index(ctx, parent, objs[i]) if err != nil { log.Errorf("update search index error while index new node: %+v", err) return