Skip to content

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

:art: Prioritize exact document and heading matches in search https://github.com/siyuan-note/siyuan/issues/18270

Daniela9706b943b29
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 0f5d05cefd..082bdb9717 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -1476,6 +1476,7 @@ func buildRootIDExclusionFilter(rootIDs []string, alias ...string) (clause strin
}
func buildOrderBy(query string, method, orderBy int) string {
+ escapedQuery := strings.ReplaceAll(query, "'", "''")
switch orderBy {
case 1:
return "ORDER BY created ASC"
@@ -1495,15 +1496,23 @@ func buildOrderBy(query string, method, orderBy int) string {
if 0 != method && 1 != method {
return "ORDER BY sort ASC, updated DESC"
}
- return "ORDER BY rank" // 默认是按相关度降序
+ clause := "ORDER BY CASE " +
+ "WHEN content = '${keyword}' AND type = 'd' THEN 10 " +
+ "WHEN content = '${keyword}' AND type = 'h' THEN 20 " +
+ "ELSE 65535 END ASC, rank"
+ return strings.ReplaceAll(clause, "${keyword}", escapedQuery) // 默认是按相关度降序
default:
clause := "ORDER BY CASE " +
"WHEN name = '${keyword}' THEN 10 " +
"WHEN alias = '${keyword}' THEN 20 " +
+ "WHEN content = '${keyword}' AND type = 'd' THEN 30 " +
+ "WHEN content LIKE '%${keyword}%' AND type = 'd' THEN 40 " +
"WHEN name LIKE '%${keyword}%' THEN 50 " +
"WHEN alias LIKE '%${keyword}%' THEN 60 " +
+ "WHEN content = '${keyword}' AND type = 'h' THEN 70 " +
+ "WHEN content LIKE '%${keyword}%' AND type = 'h' THEN 80 " +
"ELSE 65535 END ASC, sort ASC, updated DESC"
- clause = strings.ReplaceAll(clause, "${keyword}", strings.ReplaceAll(query, "'", "''"))
+ clause = strings.ReplaceAll(clause, "${keyword}", escapedQuery)
return clause
}
}
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index a11f131cac..6293ce44d2 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -167,3 +167,48 @@ func TestBuildRefUsedOrderByEmpty(t *testing.T) {
t.Fatalf("空记录不应生成排序语句:%q", orderBy)
}
}
+
+func TestBuildOrderByPrioritizesExactDocumentAndHeading(t *testing.T) {
+ orderBy := buildOrderBy("数学", 0, 0)
+ assertOrderBySequence(t, orderBy,
+ "content = '数学' AND type = 'd'",
+ "content LIKE '%数学%' AND type = 'd'",
+ "content = '数学' AND type = 'h'",
+ "content LIKE '%数学%' AND type = 'h'",
+ "sort ASC",
+ )
+
+ orderBy = buildOrderBy("数学", 0, 7)
+ assertOrderBySequence(t, orderBy,
+ "content = '数学' AND type = 'd'",
+ "content = '数学' AND type = 'h'",
+ "rank",
+ )
+
+ orderBy = buildOrderBy("数学", 0, 6)
+ if strings.Contains(orderBy, "content = '数学'") {
+ t.Fatalf("按相关度升序不应将完全命中结果置顶:%q", orderBy)
+ }
+}
+
+func TestBuildOrderByEscapesKeyword(t *testing.T) {
+ orderBy := buildOrderBy("O'Reilly", 0, 7)
+ if !strings.Contains(orderBy, "content = 'O''Reilly'") {
+ t.Fatalf("排序语句中的关键词未正确转义:%q", orderBy)
+ }
+}
+
+func assertOrderBySequence(t *testing.T, orderBy string, fragments ...string) {
+ t.Helper()
+ previous := -1
+ for _, fragment := range fragments {
+ current := strings.Index(orderBy, fragment)
+ if 0 > current {
+ t.Fatalf("排序语句缺少 %q:%q", fragment, orderBy)
+ }
+ if current <= previous {
+ t.Fatalf("排序优先级顺序错误,%q 未出现在预期位置:%q", fragment, orderBy)
+ }
+ previous = current
+ }
+}