feat(search): use FULLTEXT index (close #2716 pr #2726)

This commit is contained in:
BoYanZh
2022-12-16 16:51:36 +08:00
committed by GitHub
parent 5a6b600ace
commit b3be9ef428
4 changed files with 39 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"path"
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
@ -53,13 +54,21 @@ 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))
var searchDB *gorm.DB
switch conf.Conf.Database.Type {
case "sqlite3":
keywordsClause := db.Where("1 = 1")
for _, keyword := range strings.Split(req.Keywords, " ") {
keywordsClause = keywordsClause.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword))
}
searchDB = db.Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).Where(keywordsClause)
case "mysql":
searchDB = db.Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).
Where("MATCH (name) AGAINST (? IN NATURAL LANGUAGE MODE)", req.Keywords)
case "postgres":
searchDB = db.Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).
Where("to_tsvector(name) @@ to_tsquery(?)", req.Keywords)
}
searchDB := db.Model(&model.SearchNode{}).Where(whereInParent(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")