Skip to content

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

:bug: Prioritize case-insensitive exact matches in global search https://github.com/siyuan-note/siyuan/issues/18270

Daniel5437866299db
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 69a8b36450..efc4745557 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -1578,20 +1578,24 @@ func buildOrderBy(query string, method, orderBy int) string {
if 0 != method && 1 != method {
return "ORDER BY sort ASC, updated DESC"
}
+ exactContent := buildExactSearchOrderCondition("content", query)
clause := "ORDER BY CASE " +
- "WHEN content = '${keyword}' AND type = 'd' THEN 10 " +
- "WHEN content = '${keyword}' AND type = 'h' THEN 20 " +
+ "WHEN " + exactContent + " AND type = 'd' THEN 10 " +
+ "WHEN " + exactContent + " AND type = 'h' THEN 20 " +
"ELSE 65535 END ASC, rank"
- return strings.ReplaceAll(clause, "${keyword}", escapedQuery) // 默认是按相关度降序
+ return clause // 默认是按相关度降序
default:
+ exactName := buildExactSearchOrderCondition("name", query)
+ exactAlias := buildExactSearchOrderCondition("alias", query)
+ exactContent := buildExactSearchOrderCondition("content", query)
clause := "ORDER BY CASE " +
- "WHEN name = '${keyword}' THEN 10 " +
- "WHEN alias = '${keyword}' THEN 20 " +
- "WHEN content = '${keyword}' AND type = 'd' THEN 30 " +
+ "WHEN " + exactName + " THEN 10 " +
+ "WHEN " + exactAlias + " THEN 20 " +
+ "WHEN " + exactContent + " 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 " + exactContent + " 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}", escapedQuery)
@@ -1599,6 +1603,15 @@ func buildOrderBy(query string, method, orderBy int) string {
}
}
+func buildExactSearchOrderCondition(field, query string) string {
+ escapedQuery := strings.ReplaceAll(query, "'", "''")
+ if Conf.Search.CaseSensitive {
+ return field + " = '" + escapedQuery + "'"
+ }
+ escapedQuery = strings.NewReplacer("\\", "\\\\", "%", "\\%", "_", "\\_").Replace(escapedQuery)
+ return field + " LIKE '" + escapedQuery + "' ESCAPE '\\'"
+}
+
// buildTypeFilter returns a complete SQL predicate (including outer parens)
// suitable for appending after "AND". When subTypes is empty, the result is
// equivalent to the previous "type IN (...)" behavior. When subTypes contains
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index 868d736288..19babf76d0 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -17,6 +17,7 @@
package model
import (
+ gosql "database/sql"
"slices"
"strings"
"testing"
@@ -206,6 +207,8 @@ func TestBuildRefUsedOrderByEmpty(t *testing.T) {
}
func TestBuildOrderByPrioritizesExactDocumentAndHeading(t *testing.T) {
+ setSearchCaseSensitive(t, true)
+
orderBy := buildOrderBy("数学", 0, 0)
assertOrderBySequence(t, orderBy,
"content = '数学' AND type = 'd'",
@@ -228,11 +231,82 @@ func TestBuildOrderByPrioritizesExactDocumentAndHeading(t *testing.T) {
}
}
-func TestBuildOrderByEscapesKeyword(t *testing.T) {
- orderBy := buildOrderBy("O'Reilly", 0, 7)
- if !strings.Contains(orderBy, "content = 'O''Reilly'") {
- t.Fatalf("排序语句中的关键词未正确转义:%q", orderBy)
+func TestBuildOrderByPrioritizesCaseInsensitiveExactMatches(t *testing.T) {
+ setSearchCaseSensitive(t, false)
+
+ orderBy := buildOrderBy("seo", 0, 0)
+ assertOrderBySequence(t, orderBy,
+ "name LIKE 'seo' ESCAPE '\\'",
+ "alias LIKE 'seo' ESCAPE '\\'",
+ "content LIKE 'seo' ESCAPE '\\' AND type = 'd'",
+ "content LIKE '%seo%' AND type = 'd'",
+ "content LIKE 'seo' ESCAPE '\\' AND type = 'h'",
+ "content LIKE '%seo%' AND type = 'h'",
+ "sort ASC",
+ )
+
+ orderBy = buildOrderBy("seo", 0, 7)
+ assertOrderBySequence(t, orderBy,
+ "content LIKE 'seo' ESCAPE '\\' AND type = 'd'",
+ "content LIKE 'seo' ESCAPE '\\' AND type = 'h'",
+ "rank",
+ )
+
+ orderBy = buildOrderBy("seo", 0, 6)
+ if strings.Contains(orderBy, "content LIKE 'seo'") {
+ t.Fatalf("按相关度升序不应将完全命中结果置顶:%q", orderBy)
+ }
+}
+
+func TestBuildOrderByRanksCaseInsensitiveExactContentFirst(t *testing.T) {
+ setSearchCaseSensitive(t, false)
+ testDB, err := gosql.Open("sqlite3_extended", ":memory:")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() {
+ testDB.Close()
+ })
+ if _, err = testDB.Exec("CREATE TABLE blocks (name TEXT, alias TEXT, content TEXT, type TEXT, sort INTEGER, updated TEXT)"); err != nil {
+ t.Fatal(err)
+ }
+ if _, err = testDB.Exec("INSERT INTO blocks VALUES ('', '', 'Learn seo', 'd', 0, ''), ('', '', 'SEO', 'd', 1, '')"); err != nil {
+ t.Fatal(err)
}
+
+ row := testDB.QueryRow("SELECT content FROM blocks " + buildOrderBy("seo", 0, 0) + " LIMIT 1")
+ var content string
+ if err = row.Scan(&content); err != nil {
+ t.Fatal(err)
+ }
+ if "SEO" != content {
+ t.Fatalf("忽略大小写搜索时,完全命中的内容应排在首位:%q", content)
+ }
+}
+
+func TestBuildExactSearchOrderConditionEscapesKeyword(t *testing.T) {
+ setSearchCaseSensitive(t, true)
+ condition := buildExactSearchOrderCondition("content", "O'Reilly%_\\")
+ if expected := "content = 'O''Reilly%_\\'"; expected != condition {
+ t.Fatalf("区分大小写的完全命中条件错误:got %q, want %q", condition, expected)
+ }
+
+ Conf.Search.CaseSensitive = false
+ condition = buildExactSearchOrderCondition("content", "O'Reilly%_\\")
+ if expected := "content LIKE 'O''Reilly\\%\\_\\\\' ESCAPE '\\'"; expected != condition {
+ t.Fatalf("忽略大小写的完全命中条件错误:got %q, want %q", condition, expected)
+ }
+}
+
+func setSearchCaseSensitive(t *testing.T, caseSensitive bool) {
+ t.Helper()
+ previousConf := Conf
+ Conf = NewAppConf()
+ Conf.Search = conf.NewSearch()
+ Conf.Search.CaseSensitive = caseSensitive
+ t.Cleanup(func() {
+ Conf = previousConf
+ })
}
func assertOrderBySequence(t *testing.T, orderBy string, fragments ...string) {