Compare commits

...

2 Commits

Author SHA1 Message Date
ed5843cd54 improve search 2025-09-09 10:56:35 +08:00
c0d904e035 improve search 2025-09-09 10:38:25 +08:00
2 changed files with 25 additions and 18 deletions

View File

@@ -430,24 +430,32 @@ func GetResourcesIdWithTag(tagID uint) ([]uint, error) {
} }
func BatchGetResources(ids []uint) ([]model.Resource, error) { func BatchGetResources(ids []uint) ([]model.Resource, error) {
idMap := make(map[uint]struct{}) var resources []model.Resource
uniqueIds := make([]uint, 0, len(ids))
for _, id := range ids { for _, id := range ids {
if _, exists := idMap[id]; !exists { var r model.Resource
idMap[id] = struct{}{} if err := db.
uniqueIds = append(uniqueIds, id) Preload("User").
Preload("Images").
Preload("Tags").
First(&r, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
continue
}
return nil, err
} }
for i, tag := range r.Tags {
if tag.AliasOf != nil {
t, err := GetTagByID(*tag.AliasOf)
if err != nil {
return nil, err
} else {
r.Tags[i].Type = t.Type
}
}
}
resources = append(resources, r)
} }
var resources []model.Resource
if err := db.
Where("id IN ?", uniqueIds).
Preload("User").
Preload("Images").
Preload("Tags").
Find(&resources).
Error; err != nil {
return nil, err
}
return resources, nil return resources, nil
} }

View File

@@ -82,7 +82,6 @@ func init() {
func SearchResource(keyword string) ([]uint, error) { func SearchResource(keyword string) ([]uint, error) {
query := bleve.NewMatchQuery(keyword) query := bleve.NewMatchQuery(keyword)
searchRequest := bleve.NewSearchRequest(query) searchRequest := bleve.NewSearchRequest(query)
searchRequest.Size = 1000
searchResults, err := index.Search(searchRequest) searchResults, err := index.Search(searchRequest)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -90,8 +89,8 @@ func SearchResource(keyword string) ([]uint, error) {
results := make([]uint, 0) results := make([]uint, 0)
for _, hit := range searchResults.Hits { for _, hit := range searchResults.Hits {
if hit.Score < 0.6 { if hit.Score < 0.2 {
continue break
} }
id, err := strconv.ParseUint(hit.ID, 10, 32) id, err := strconv.ParseUint(hit.ID, 10, 32)
if err != nil { if err != nil {