mirror of https://github.com/Xhofe/alist
feat(search): multiple keywords split by space (#2669)
parent
62ea93837c
commit
1640f06e13
|
@ -3,6 +3,7 @@ package db
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"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) {
|
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(
|
searchDB := db.Model(&model.SearchNode{}).Where(
|
||||||
fmt.Sprintf("%s LIKE ? AND %s LIKE ?",
|
fmt.Sprintf("%s LIKE ?", columnName("parent")),
|
||||||
columnName("parent"),
|
fmt.Sprintf("%s%%", req.Parent)).Where(keywordsClause)
|
||||||
columnName("name")),
|
|
||||||
fmt.Sprintf("%s%%", req.Parent),
|
|
||||||
fmt.Sprintf("%%%s%%", req.Keywords))
|
|
||||||
var count int64
|
var count int64
|
||||||
if err := searchDB.Count(&count).Error; err != nil {
|
if err := searchDB.Count(&count).Error; err != nil {
|
||||||
return nil, 0, errors.Wrapf(err, "failed get users count")
|
return nil, 0, errors.Wrapf(err, "failed get users count")
|
||||||
|
|
Loading…
Reference in New Issue