This commit is contained in:
Daniel 2025-07-08 20:27:22 +08:00
parent 03a10b7c88
commit ed6a90ab84
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -272,10 +272,10 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) { func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
ret = []*cache.Asset{} ret = []*cache.Asset{}
keywords := strings.Split(keyword, " ") var keywords []string
keywords = append(keywords, keyword)
keywords = append(keywords, strings.Split(keyword, " ")...)
pathHitCount := map[string]int{} pathHitCount := map[string]int{}
count := 0
filterByExt := 0 < len(exts) filterByExt := 0 < len(exts)
for _, asset := range cache.GetAssets() { for _, asset := range cache.GetAssets() {
if filterByExt { if filterByExt {
@ -295,8 +295,18 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
lowerHName := strings.ToLower(asset.HName) lowerHName := strings.ToLower(asset.HName)
lowerPath := strings.ToLower(asset.Path) lowerPath := strings.ToLower(asset.Path)
var hitNameCount, hitPathCount int var hitNameCount, hitPathCount int
for _, k := range keywords { for i, k := range keywords {
lowerKeyword := strings.ToLower(k) lowerKeyword := strings.ToLower(k)
if 0 == i {
// 第一个是完全匹配,权重最高
if strings.Contains(lowerHName, lowerKeyword) {
hitNameCount += 64
}
if strings.Contains(lowerPath, lowerKeyword) {
hitPathCount += 64
}
}
hitNameCount += strings.Count(lowerHName, lowerKeyword) hitNameCount += strings.Count(lowerHName, lowerKeyword)
hitPathCount += strings.Count(lowerPath, lowerKeyword) hitPathCount += strings.Count(lowerPath, lowerKeyword)
if 1 > hitNameCount && 1 > hitPathCount { if 1 > hitNameCount && 1 > hitPathCount {
@ -318,10 +328,6 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
Path: asset.Path, Path: asset.Path,
Updated: asset.Updated, Updated: asset.Updated,
}) })
count++
if Conf.Search.Limit <= count {
return
}
} }
if 0 < len(pathHitCount) { if 0 < len(pathHitCount) {
@ -333,6 +339,10 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
return ret[i].Updated > ret[j].Updated return ret[i].Updated > ret[j].Updated
}) })
} }
if Conf.Search.Limit <= len(ret) {
ret = ret[:Conf.Search.Limit]
}
return return
} }