Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve assets searching https://github.com/siyuan-note/siyuan/issues/15251

Danieled6a90ab8443
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 2cba74ac01..d65e192baa 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -272,10 +272,10 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
func SearchAssetsByName(keyword string, exts []string) (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{}
- count := 0
filterByExt := 0 < len(exts)
for _, asset := range cache.GetAssets() {
if filterByExt {
@@ -295,8 +295,18 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
lowerHName := strings.ToLower(asset.HName)
lowerPath := strings.ToLower(asset.Path)
var hitNameCount, hitPathCount int
- for _, k := range keywords {
+ for i, k := range keywords {
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)
hitPathCount += strings.Count(lowerPath, lowerKeyword)
if 1 > hitNameCount && 1 > hitPathCount {
@@ -318,10 +328,6 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
Path: asset.Path,
Updated: asset.Updated,
})
- count++
- if Conf.Search.Limit <= count {
- return
- }
}
if 0 < len(pathHitCount) {
@@ -333,6 +339,10 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
return ret[i].Updated > ret[j].Updated
})
}
+
+ if Conf.Search.Limit <= len(ret) {
+ ret = ret[:Conf.Search.Limit]
+ }
return
}