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:
@ -2,53 +2,49 @@ package handles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/index"
|
||||
"github.com/alist-org/alist/v3/internal/search"
|
||||
"github.com/alist-org/alist/v3/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type BuildIndexReq struct {
|
||||
Paths []string `json:"paths"`
|
||||
MaxDepth int `json:"max_depth"`
|
||||
IgnorePaths []string `json:"ignore_paths"`
|
||||
}
|
||||
|
||||
func BuildIndex(c *gin.Context) {
|
||||
var req BuildIndexReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if search.Running {
|
||||
common.ErrorStrResp(c, "index is running", 400)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
// TODO: consider run build index as non-admin
|
||||
user, _ := db.GetAdmin()
|
||||
ctx := context.WithValue(c.Request.Context(), "user", user)
|
||||
maxDepth, err := strconv.Atoi(c.PostForm("max_depth"))
|
||||
ctx := context.Background()
|
||||
err := search.Clear(ctx)
|
||||
if err != nil {
|
||||
maxDepth = -1
|
||||
log.Errorf("clear index error: %+v", err)
|
||||
return
|
||||
}
|
||||
err = search.BuildIndex(context.Background(), req.Paths, req.IgnorePaths, req.MaxDepth, true)
|
||||
if err != nil {
|
||||
log.Errorf("build index error: %+v", err)
|
||||
}
|
||||
indexPaths := []string{"/"}
|
||||
ignorePaths := c.PostFormArray("ignore_paths")
|
||||
index.BuildIndex(ctx, indexPaths, ignorePaths, maxDepth)
|
||||
}()
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
func GetProgress(c *gin.Context) {
|
||||
progress := index.ReadProgress()
|
||||
common.SuccessResp(c, progress)
|
||||
}
|
||||
|
||||
func Search(c *gin.Context) {
|
||||
results := []string{}
|
||||
query, exists := c.GetQuery("query")
|
||||
if !exists {
|
||||
common.SuccessResp(c, results)
|
||||
}
|
||||
sizeStr, _ := c.GetQuery("size")
|
||||
size, err := strconv.Atoi(sizeStr)
|
||||
if err != nil {
|
||||
size = 10
|
||||
}
|
||||
searchResults, err := index.Search(query, size)
|
||||
progress, err := search.Progress(c)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
for _, documentMatch := range searchResults.Hits {
|
||||
results = append(results, documentMatch.Fields["Path"].(string))
|
||||
}
|
||||
common.SuccessResp(c, results)
|
||||
common.SuccessResp(c, progress)
|
||||
}
|
||||
|
Reference in New Issue
Block a user