mirror of https://github.com/Xhofe/alist
Co-authored-by: Andy Hsu <i@nn.ci>pull/4937/head
parent
3686075a7f
commit
1d06a0019f
|
@ -72,12 +72,19 @@ func SearchNode(req model.SearchReq, useFullText bool) ([]model.SearchNode, int6
|
|||
Where("to_tsvector(name) @@ to_tsquery(?)", strings.Join(strings.Fields(req.Keywords), " & "))
|
||||
}
|
||||
}
|
||||
|
||||
if req.Scope != 0 {
|
||||
isDir := req.Scope == 1
|
||||
searchDB.Where(db.Where("is_dir = ?", isDir))
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := searchDB.Count(&count).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get search items count")
|
||||
}
|
||||
var files []model.SearchNode
|
||||
if err := searchDB.Offset((req.Page - 1) * req.PerPage).Limit(req.PerPage).Find(&files).Error; err != nil {
|
||||
if err := searchDB.Order("name asc").Offset((req.Page - 1) * req.PerPage).Limit(req.PerPage).
|
||||
Find(&files).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return files, count, nil
|
||||
|
|
|
@ -15,6 +15,8 @@ type IndexProgress struct {
|
|||
type SearchReq struct {
|
||||
Parent string `json:"parent"`
|
||||
Keywords string `json:"keywords"`
|
||||
// 0 for all, 1 for dir, 2 for file
|
||||
Scope int `json:"scope"`
|
||||
PageReq
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"os"
|
||||
|
||||
query2 "github.com/blevesearch/bleve/v2/search/query"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
|
@ -24,9 +26,19 @@ func (b *Bleve) Config() searcher.Config {
|
|||
}
|
||||
|
||||
func (b *Bleve) Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) {
|
||||
var queries []query2.Query
|
||||
query := bleve.NewMatchQuery(req.Keywords)
|
||||
query.SetField("name")
|
||||
search := bleve.NewSearchRequest(query)
|
||||
queries = append(queries, query)
|
||||
if req.Scope != 0 {
|
||||
isDir := req.Scope == 1
|
||||
isDirQuery := bleve.NewBoolFieldQuery(isDir)
|
||||
queries = append(queries, isDirQuery)
|
||||
}
|
||||
reqQuery := bleve.NewConjunctionQuery(queries...)
|
||||
search := bleve.NewSearchRequest(reqQuery)
|
||||
search.SortBy([]string{"name"})
|
||||
search.From = (req.Page - 1) * req.PerPage
|
||||
search.Size = req.PerPage
|
||||
search.Fields = []string{"*"}
|
||||
searchResults, err := b.BIndex.Search(search)
|
||||
|
@ -42,7 +54,7 @@ func (b *Bleve) Search(ctx context.Context, req model.SearchReq) ([]model.Search
|
|||
Size: int64(src.Fields["size"].(float64)),
|
||||
}, nil
|
||||
})
|
||||
return res, int64(len(res)), nil
|
||||
return res, int64(searchResults.Total), nil
|
||||
}
|
||||
|
||||
func (b *Bleve) Index(ctx context.Context, node model.SearchNode) error {
|
||||
|
|
Loading…
Reference in New Issue