From 24ba97817ae3a0c8db26fc7b5cabd4c1eb4fb88d Mon Sep 17 00:00:00 2001 From: nyne Date: Mon, 8 Sep 2025 21:59:55 +0800 Subject: [PATCH] Improve search --- server/search/resource.go | 3 --- server/service/resource.go | 9 +++++++-- server/utils/string.go | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/server/search/resource.go b/server/search/resource.go index 4fae6dd..6c6aaf2 100644 --- a/server/search/resource.go +++ b/server/search/resource.go @@ -7,7 +7,6 @@ import ( "nysoure/server/model" "nysoure/server/utils" "strconv" - "strings" "time" "github.com/blevesearch/bleve" @@ -81,8 +80,6 @@ func init() { } func SearchResource(keyword string) ([]uint, error) { - keyword = strings.Replace(keyword, "-", " ", -1) - query := bleve.NewMatchQuery(keyword) searchRequest := bleve.NewSearchRequest(query) searchRequest.Size = 1000 diff --git a/server/service/resource.go b/server/service/resource.go index aaa5ade..c801331 100644 --- a/server/service/resource.go +++ b/server/service/resource.go @@ -1,8 +1,6 @@ package service import ( - "github.com/gofiber/fiber/v3" - "github.com/gofiber/fiber/v3/log" "net/url" "nysoure/server/config" "nysoure/server/dao" @@ -12,6 +10,9 @@ import ( "strconv" "strings" + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/log" + "gorm.io/gorm" ) @@ -295,6 +296,10 @@ func SearchResource(query string, page int) ([]model.ResourceView, int, error) { if keyword == "" { continue } + if utils.OnlyPunctuation(keyword) { + continue + } + res, err := searchWithKeyword(keyword) if err != nil { return nil, 0, err diff --git a/server/utils/string.go b/server/utils/string.go index 6a0e6d2..fdbd9d3 100644 --- a/server/utils/string.go +++ b/server/utils/string.go @@ -1,8 +1,21 @@ package utils -import "regexp" +import ( + "regexp" + "unicode" +) func RemoveSpaces(s string) string { reg := regexp.MustCompile(`\s+`) return reg.ReplaceAllString(s, " ") } + +func OnlyPunctuation(s string) bool { + for _, r := range s { + if unicode.IsPunct(r) || unicode.IsSpace(r) { + continue + } + return false + } + return true +}