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

This commit is contained in:
Daniel 2024-04-09 22:26:37 +08:00
parent 7d680ed391
commit b3a2c49f15
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 141 additions and 41 deletions

View file

@ -208,7 +208,7 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
logging.LogErrorf("read directory [%s] failed: %s", avDir, err)
return
}
avBlockRels := av.GetBlockRels()
for _, entry := range entries {
if entry.IsDir() {
continue
@ -219,6 +219,10 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
continue
}
if nil == avBlockRels[id] {
continue
}
name, _ := av.GetAttributeViewNameByPath(filepath.Join(avDir, entry.Name()))
info, _ := entry.Info()
if "" != keyword {
@ -237,23 +241,33 @@ func SearchAttributeView(keyword string) (ret []*SearchAttributeViewResult) {
}
avs = append(avs, a)
}
}
if "" == keyword {
sort.Slice(avs, func(i, j int) bool { return avs[i].AvUpdated > avs[j].AvUpdated })
} else {
sort.Slice(avs, func(i, j int) bool { return avs[i].Score > avs[j].Score })
sort.SliceStable(avs, func(i, j int) bool {
if avs[i].Score == avs[j].Score {
return avs[i].AvUpdated > avs[j].AvUpdated
}
return avs[i].Score > avs[j].Score
})
}
if 16 < len(avs) {
avs = avs[:16]
if 12 <= len(avs) {
avs = avs[:12]
}
var avIDs []string
for _, a := range avs {
avIDs = append(avIDs, a.AvID)
}
blockIDs := treenode.BatchGetMirrorAttrViewBlockIDs(avIDs)
avBlocks := treenode.BatchGetMirrorAttrViewBlocks(avIDs)
var blockIDs []string
for _, avBlock := range avBlocks {
blockIDs = append(blockIDs, avBlock.BlockIDs...)
}
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
trees := map[string]*parse.Tree{}
for _, blockID := range blockIDs {
bt := treenode.GetBlockTree(blockID)