fix: pgsql search [skip ci] (close #2761 pr #2774)

pull/2785/head
BoYanZh 2022-12-21 19:19:37 +08:00 committed by GitHub
parent 105f22969c
commit 3a41b929c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -59,7 +59,7 @@ func SearchNode(req model.SearchReq) ([]model.SearchNode, int64, error) {
switch conf.Conf.Database.Type {
case "sqlite3":
keywordsClause := db.Where("1 = 1")
for _, keyword := range strings.Split(req.Keywords, " ") {
for _, keyword := range strings.Fields(req.Keywords) {
keywordsClause = keywordsClause.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword))
}
searchDB = db.Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).Where(keywordsClause)
@ -68,7 +68,7 @@ func SearchNode(req model.SearchReq) ([]model.SearchNode, int64, error) {
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)
Where("to_tsvector(name) @@ to_tsquery(?)", strings.Join(strings.Fields(req.Keywords), " & "))
}
var count int64
if err := searchDB.Count(&count).Error; err != nil {