mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Improve search
This commit is contained in:
@@ -440,7 +440,13 @@ func BatchGetResources(ids []uint) ([]model.Resource, error) {
|
||||
}
|
||||
|
||||
var resources []model.Resource
|
||||
if err := db.Where("id IN ?", uniqueIds).Find(&resources).Error; err != nil {
|
||||
if err := db.
|
||||
Where("id IN ?", uniqueIds).
|
||||
Preload("User").
|
||||
Preload("Images").
|
||||
Preload("Tags").
|
||||
Find(&resources).
|
||||
Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resources, nil
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/utils"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -21,11 +20,6 @@ type ResourceParams struct {
|
||||
|
||||
var index bleve.Index
|
||||
|
||||
func removeSpaces(s string) string {
|
||||
reg := regexp.MustCompile(`\s+`)
|
||||
return reg.ReplaceAllString(s, " ")
|
||||
}
|
||||
|
||||
func createIndex() error {
|
||||
for !dao.IsReady() {
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -39,10 +33,10 @@ func createIndex() error {
|
||||
}
|
||||
for _, r := range res {
|
||||
title := r.Title
|
||||
title = removeSpaces(title)
|
||||
title = utils.RemoveSpaces(title)
|
||||
altTitles := make([]string, len(r.AlternativeTitles))
|
||||
for i, t := range r.AlternativeTitles {
|
||||
altTitles[i] = removeSpaces(t)
|
||||
altTitles[i] = utils.RemoveSpaces(t)
|
||||
}
|
||||
err := index.Index(fmt.Sprintf("%d", r.ID), ResourceParams{
|
||||
Id: r.ID,
|
||||
@@ -78,7 +72,7 @@ func init() {
|
||||
}
|
||||
|
||||
func SearchResource(keyword string) (map[uint]time.Time, error) {
|
||||
keyword = removeSpaces(keyword)
|
||||
keyword = utils.RemoveSpaces(keyword)
|
||||
|
||||
query := bleve.NewMatchQuery(keyword)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/search"
|
||||
"nysoure/server/utils"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -272,7 +273,7 @@ func SearchResource(query string, page int) ([]model.ResourceView, int, error) {
|
||||
}
|
||||
|
||||
// check tag after removing spaces
|
||||
trimmed := strings.ReplaceAll(query, " ", "")
|
||||
trimmed := utils.RemoveSpaces(query)
|
||||
if trimmed != query {
|
||||
if err := checkTag(trimmed); err != nil {
|
||||
return nil, 0, err
|
||||
|
8
server/utils/string.go
Normal file
8
server/utils/string.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
func RemoveSpaces(s string) string {
|
||||
reg := regexp.MustCompile(`\s+`)
|
||||
return reg.ReplaceAllString(s, " ")
|
||||
}
|
Reference in New Issue
Block a user