Skip to content

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

:bug: Prioritize recently referenced blocks in search https://github.com/siyuan-note/siyuan/issues/18226

Danielc643dd94f8f7
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 377724a226..7185d524e0 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -1716,7 +1716,7 @@ func fullTextSearchRefBlockInBox(keyword string, beforeLen int, onlyDoc bool, bo
stmt += buf.String()
}
- orderBy := ` ORDER BY CASE
+ orderBy := " ORDER BY " + buildRefUsedOrderBy(GetRefUsed()) + `CASE
WHEN name = '${keyword}' THEN 10
WHEN alias = '${keyword}' THEN 20
WHEN memo = '${keyword}' THEN 30
@@ -1741,6 +1741,44 @@ func fullTextSearchRefBlockInBox(keyword string, beforeLen int, onlyDoc bool, bo
return
}
+func buildRefUsedOrderBy(refUsed map[string]int64) string {
+ type refUsedEntry struct {
+ id string
+ timestamp int64
+ }
+
+ entries := make([]refUsedEntry, 0, len(refUsed))
+ for id, timestamp := range refUsed {
+ if 22 == len(id) && ast.IsNodeIDPattern(id) {
+ entries = append(entries, refUsedEntry{id: id, timestamp: timestamp})
+ }
+ }
+ if 1 > len(entries) {
+ return ""
+ }
+
+ sort.Slice(entries, func(i, j int) bool {
+ if entries[i].timestamp == entries[j].timestamp {
+ return entries[i].id > entries[j].id
+ }
+ return entries[i].timestamp > entries[j].timestamp
+ })
+
+ buf := bytes.Buffer{}
+ buf.WriteString("CASE id ")
+ for i, entry := range entries {
+ buf.WriteString("WHEN '")
+ buf.WriteString(entry.id)
+ buf.WriteString("' THEN ")
+ buf.WriteString(strconv.Itoa(i))
+ buf.WriteByte(' ')
+ }
+ buf.WriteString("ELSE ")
+ buf.WriteString(strconv.Itoa(len(entries)))
+ buf.WriteString(" END ASC, ")
+ return buf.String()
+}
+
func extractID(content string) (ret string) {
// Improve block ref search ID extraction https://github.com/siyuan-note/siyuan/issues/10848
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index 1140c2733e..f9b927ecb0 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -16,7 +16,10 @@
package model
-import "testing"
+import (
+ "strings"
+ "testing"
+)
// TestIsValidSearchBoxPath 覆盖搜索入参的笔记本 ID 与文档路径校验,阻止 SQL 元字符进入语句拼接。
// 回归用例参考 /api/search/fullTextSearchBlock 的 SQL 注入报告(paths[] 投毒)。
@@ -107,3 +110,32 @@ func countPlaceholder(s string) (n int) {
}
return
}
+
+func TestBuildRefUsedOrderBy(t *testing.T) {
+ newestID := "20260714120000-newest1"
+ olderID := "20260714110000-older01"
+ invalidID := "invalid-id' OR 1=1 --"
+ orderBy := buildRefUsedOrderBy(map[string]int64{
+ olderID: 100,
+ newestID: 200,
+ invalidID: 300,
+ })
+
+ newestPos := strings.Index(orderBy, newestID)
+ olderPos := strings.Index(orderBy, olderID)
+ if 0 > newestPos || 0 > olderPos || newestPos >= olderPos {
+ t.Fatalf("最近引用块应排在较早引用块之前:%q", orderBy)
+ }
+ if strings.Contains(orderBy, invalidID) {
+ t.Fatalf("排序语句不应包含非法块 ID:%q", orderBy)
+ }
+ if !strings.HasSuffix(orderBy, "END ASC, ") {
+ t.Fatalf("排序语句格式错误:%q", orderBy)
+ }
+}
+
+func TestBuildRefUsedOrderByEmpty(t *testing.T) {
+ if orderBy := buildRefUsedOrderBy(nil); "" != orderBy {
+ t.Fatalf("空记录不应生成排序语句:%q", orderBy)
+ }
+}