mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 20:27:23 +00:00
Improve search
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"nysoure/server/model"
|
"nysoure/server/model"
|
||||||
"nysoure/server/utils"
|
"nysoure/server/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/blevesearch/bleve"
|
"github.com/blevesearch/bleve"
|
||||||
@@ -81,8 +80,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SearchResource(keyword string) ([]uint, error) {
|
func SearchResource(keyword string) ([]uint, error) {
|
||||||
keyword = strings.Replace(keyword, "-", " ", -1)
|
|
||||||
|
|
||||||
query := bleve.NewMatchQuery(keyword)
|
query := bleve.NewMatchQuery(keyword)
|
||||||
searchRequest := bleve.NewSearchRequest(query)
|
searchRequest := bleve.NewSearchRequest(query)
|
||||||
searchRequest.Size = 1000
|
searchRequest.Size = 1000
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v3"
|
|
||||||
"github.com/gofiber/fiber/v3/log"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"nysoure/server/config"
|
"nysoure/server/config"
|
||||||
"nysoure/server/dao"
|
"nysoure/server/dao"
|
||||||
@@ -12,6 +10,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v3"
|
||||||
|
"github.com/gofiber/fiber/v3/log"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -295,6 +296,10 @@ func SearchResource(query string, page int) ([]model.ResourceView, int, error) {
|
|||||||
if keyword == "" {
|
if keyword == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if utils.OnlyPunctuation(keyword) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
res, err := searchWithKeyword(keyword)
|
res, err := searchWithKeyword(keyword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
@@ -1,8 +1,21 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
func RemoveSpaces(s string) string {
|
func RemoveSpaces(s string) string {
|
||||||
reg := regexp.MustCompile(`\s+`)
|
reg := regexp.MustCompile(`\s+`)
|
||||||
return reg.ReplaceAllString(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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user