🎨 Improve Add to Database search sorting https://github.com/siyuan-note/siyuan/issues/10952

This commit is contained in:
Daniel 2024-04-09 17:51:39 +08:00
parent a9899e30cf
commit 225930d3cf
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 37 additions and 14 deletions

View file

@ -36,6 +36,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/cache" "github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
"github.com/xrash/smetrics"
) )
func SetDatabaseBlockView(blockID, viewID string) (err error) { func SetDatabaseBlockView(blockID, viewID string) (err error) {
@ -194,7 +195,12 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
ret = []*SearchAttributeViewResult{} ret = []*SearchAttributeViewResult{}
keyword = strings.TrimSpace(keyword) keyword = strings.TrimSpace(keyword)
avs := map[string]string{} type result struct {
AvID string
AvName string
Score float64
}
var avs []*result
avDir := filepath.Join(util.DataDir, "storage", "av") avDir := filepath.Join(util.DataDir, "storage", "av")
const limit = 16 const limit = 16
entries, err := os.ReadDir(avDir) entries, err := os.ReadDir(avDir)
@ -220,17 +226,23 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
} }
if strings.Contains(strings.ToLower(name), strings.ToLower(keyword)) { if strings.Contains(strings.ToLower(name), strings.ToLower(keyword)) {
avs[id] = name score := smetrics.JaroWinkler(name, keyword, 0.7, 4)
avs = append(avs, &result{AvID: id, AvName: name, Score: score})
count++ count++
if limit <= count { if "" == keyword && limit <= count {
break break
} }
} }
} }
sort.Slice(avs, func(i, j int) bool { return avs[i].Score > avs[j].Score })
if limit < len(avs) {
avs = avs[:limit]
}
var avIDs []string var avIDs []string
for avID := range avs { for _, av := range avs {
avIDs = append(avIDs, avID) avIDs = append(avIDs, av.AvID)
} }
blockIDs := treenode.BatchGetMirrorAttrViewBlockIDs(avIDs) blockIDs := treenode.BatchGetMirrorAttrViewBlockIDs(avIDs)
trees := map[string]*parse.Tree{} trees := map[string]*parse.Tree{}
@ -261,8 +273,14 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
} }
avID := node.AttributeViewID avID := node.AttributeViewID
name := avs[avID] var existAv *result
if "" == name { for _, av := range avs {
if av.AvID == avID {
existAv = av
break
}
}
if nil == existAv {
continue continue
} }
@ -287,7 +305,7 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
if !exist { if !exist {
ret = append(ret, &SearchAttributeViewResult{ ret = append(ret, &SearchAttributeViewResult{
AvID: avID, AvID: avID,
AvName: name, AvName: existAv.AvName,
BlockID: blockID, BlockID: blockID,
HPath: hPath, HPath: hPath,
}) })

View file

@ -384,18 +384,23 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets
} }
} }
if "NodeAttributeView" == b.Type {
// 数据库块可以添加到自身数据库块中,当前文档也可以添加到自身数据库块中
tmp = append(tmp, b)
} else {
// 排除自身块、父块和根块
if b.ID != id && !hitFirstChildID && b.ID != rootID { if b.ID != id && !hitFirstChildID && b.ID != rootID {
tmp = append(tmp, b) tmp = append(tmp, b)
} }
} }
}
ret = tmp ret = tmp
if "" != keyword {
if block := treenode.GetBlockTree(id); nil != block { if block := treenode.GetBlockTree(id); nil != block {
p := path.Join(block.HPath, keyword) p := path.Join(block.HPath, keyword)
newDoc = nil == treenode.GetBlockTreeRootByHPath(block.BoxID, p) newDoc = nil == treenode.GetBlockTreeRootByHPath(block.BoxID, p)
} }
}
// 在 hPath 中加入笔记本名 Show notebooks in hpath of block ref search list results https://github.com/siyuan-note/siyuan/issues/9378 // 在 hPath 中加入笔记本名 Show notebooks in hpath of block ref search list results https://github.com/siyuan-note/siyuan/issues/9378
prependNotebookNameInHPath(ret) prependNotebookNameInHPath(ret)