This commit is contained in:
Daniel 2024-11-25 21:51:40 +08:00
parent 4335a464f6
commit 497b7829f1
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -249,7 +249,9 @@ 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, " ")
pathHitCount := map[string]int{}
count := 0 count := 0
filterByExt := 0 < len(exts) filterByExt := 0 < len(exts)
for _, asset := range cache.GetAssets() { for _, asset := range cache.GetAssets() {
@ -269,16 +271,24 @@ 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)
lowerKeyword := strings.ToLower(keyword) var hitNameCount, hitPathCount int
hitName := strings.Contains(lowerHName, lowerKeyword) for _, k := range keywords {
hitPath := strings.Contains(lowerPath, lowerKeyword) lowerKeyword := strings.ToLower(k)
if !hitName && !hitPath { hitNameCount += strings.Count(lowerHName, lowerKeyword)
continue hitPathCount += strings.Count(lowerPath, lowerKeyword)
if 1 > hitNameCount && 1 > hitPathCount {
continue
}
} }
if 1 > hitNameCount+hitPathCount {
continue
}
pathHitCount[asset.Path] += hitNameCount + hitPathCount
hName := asset.HName hName := asset.HName
if hitName { if 0 < hitNameCount {
_, hName = search.MarkText(asset.HName, keyword, 64, Conf.Search.CaseSensitive) _, hName = search.MarkText(asset.HName, strings.Join(keywords, search.TermSep), 64, Conf.Search.CaseSensitive)
} }
ret = append(ret, &cache.Asset{ ret = append(ret, &cache.Asset{
HName: hName, HName: hName,
@ -291,9 +301,15 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
} }
} }
sort.Slice(ret, func(i, j int) bool { if 0 < len(pathHitCount) {
return ret[i].Updated > ret[j].Updated sort.Slice(ret, func(i, j int) bool {
}) return pathHitCount[ret[i].Path] > pathHitCount[ret[j].Path]
})
} else {
sort.Slice(ret, func(i, j int) bool {
return ret[i].Updated > ret[j].Updated
})
}
return return
} }