feat(search): multiple keywords split by space (#2669)

pull/2686/head
BoYanZh 2022-12-10 19:28:34 +08:00 committed by GitHub
parent 62ea93837c
commit 1640f06e13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package db
import (
"fmt"
"path"
"strings"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
@ -44,12 +45,15 @@ func GetSearchNodesByParent(parent string) ([]model.SearchNode, error) {
}
func SearchNode(req model.SearchReq) ([]model.SearchNode, int64, error) {
keywordsClause := db.Where("1 = 1")
for _, keyword := range strings.Split(req.Keywords, " ") {
keywordsClause = keywordsClause.Where(
fmt.Sprintf("%s LIKE ?", columnName("name")),
fmt.Sprintf("%%%s%%", keyword))
}
searchDB := db.Model(&model.SearchNode{}).Where(
fmt.Sprintf("%s LIKE ? AND %s LIKE ?",
columnName("parent"),
columnName("name")),
fmt.Sprintf("%s%%", req.Parent),
fmt.Sprintf("%%%s%%", req.Keywords))
fmt.Sprintf("%s LIKE ?", columnName("parent")),
fmt.Sprintf("%s%%", req.Parent)).Where(keywordsClause)
var count int64
if err := searchDB.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get users count")