Skip to content

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

:bug: Restore whitespace search https://github.com/siyuan-note/siyuan/issues/18782

Daniel435991e2c3c1
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 4b1888d528..2fa3ea3a0c 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -1708,12 +1708,11 @@ func FullTextSearchBlockInBoxWithHPathContext(ctx context.Context, query string,
blocks, matchedBlockCount, matchedRootCount = searchBySQLInBox("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize, boxID)
} else {
if 2 > len(strings.Split(strings.TrimSpace(query), " ")) {
- rawQuery := strings.TrimSpace(query)
- query = stringQuery(rawQuery)
- if "" != rawQuery && searchHPath && isDocumentSearchEnabled(types) {
- blocks, matchedBlockCount, matchedRootCount = fullTextSearchByFTSAndHPathInBox(rawQuery, query, boxFilter, pathFilter, boxArgs, pathArgs, typeFilter, ignoreFilter, orderBy, beforeLen, page, pageSize, boxID)
+ ftsQuery, hPathQuery := buildKeywordSearchQueries(query)
+ if "" != hPathQuery && searchHPath && isDocumentSearchEnabled(types) {
+ blocks, matchedBlockCount, matchedRootCount = fullTextSearchByFTSAndHPathInBox(hPathQuery, ftsQuery, boxFilter, pathFilter, boxArgs, pathArgs, typeFilter, ignoreFilter, orderBy, beforeLen, page, pageSize, boxID)
} else {
- blocks, matchedBlockCount, matchedRootCount = fullTextSearchByFTSInBox(query, boxFilter, pathFilter, boxArgs, pathArgs, typeFilter, ignoreFilter, orderByClause, beforeLen, page, pageSize, boxID)
+ blocks, matchedBlockCount, matchedRootCount = fullTextSearchByFTSInBox(ftsQuery, boxFilter, pathFilter, boxArgs, pathArgs, typeFilter, ignoreFilter, orderByClause, beforeLen, page, pageSize, boxID)
}
} else {
docMode = true // 文档全文搜索模式 https://github.com/siyuan-note/siyuan/issues/10584
@@ -3177,6 +3176,10 @@ func columnConcat() string {
return buf.String()
}
+func buildKeywordSearchQueries(query string) (ftsQuery, hPathQuery string) {
+ return stringQuery(query), strings.TrimSpace(query)
+}
+
func stringQuery(query string) string {
trimmedQuery := strings.TrimSpace(query)
if "" == trimmedQuery {
diff --git a/kernel/model/search_hpath_fts_test.go b/kernel/model/search_hpath_fts_test.go
index 129d947478..553a91aab5 100644
--- a/kernel/model/search_hpath_fts_test.go
+++ b/kernel/model/search_hpath_fts_test.go
@@ -103,3 +103,57 @@ func TestFTSAndHPathMatchesIntegration(t *testing.T) {
t.Fatal(err)
}
}
+
+func TestSiYuanFTSMatchesWhitespace(t *testing.T) {
+ testDB, err := gosql.Open("sqlite3_extended", ":memory:")
+ if err != nil {
+ t.Fatal(err)
+ }
+ testDB.SetMaxOpenConns(1)
+ t.Cleanup(func() {
+ testDB.Close()
+ })
+ if _, err = testDB.Exec(`CREATE VIRTUAL TABLE whitespace_fts USING fts5(content, tokenize="siyuan")`); err != nil {
+ t.Fatal(err)
+ }
+ for _, content := range []string{"你好", "你 好", "你 好"} {
+ if _, err = testDB.Exec("INSERT INTO whitespace_fts(content) VALUES (?)", content); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ assertMatches := func(query string, expected []string) {
+ t.Helper()
+ ftsQuery, hPathQuery := buildKeywordSearchQueries(query)
+ if "" != hPathQuery {
+ t.Fatalf("纯空白查询不应参与层级路径搜索:%q", hPathQuery)
+ }
+ matchQuery := "{content}:(" + ftsQuery + ")"
+ rows, queryErr := testDB.Query("SELECT content FROM whitespace_fts WHERE whitespace_fts MATCH ? ORDER BY rowid", matchQuery)
+ if queryErr != nil {
+ t.Fatal(queryErr)
+ }
+ var actual []string
+ for rows.Next() {
+ var content string
+ if queryErr = rows.Scan(&content); queryErr != nil {
+ rows.Close()
+ t.Fatal(queryErr)
+ }
+ actual = append(actual, content)
+ }
+ if queryErr = rows.Err(); queryErr != nil {
+ rows.Close()
+ t.Fatal(queryErr)
+ }
+ if queryErr = rows.Close(); queryErr != nil {
+ t.Fatal(queryErr)
+ }
+ if !slices.Equal(actual, expected) {
+ t.Fatalf("空白查询 %q 的匹配结果错误:got %v, want %v", query, actual, expected)
+ }
+ }
+
+ assertMatches(" ", []string{"你 好", "你 好"})
+ assertMatches(" ", []string{"你 好"})
+}
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index cc28beba20..b322fdbb2c 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -347,6 +347,34 @@ func TestBuildExactSearchOrderConditionEscapesKeyword(t *testing.T) {
}
}
+func TestBuildKeywordSearchQueries(t *testing.T) {
+ tests := []struct {
+ name string
+ query string
+ expectedFTS string
+ expectedHPath string
+ }{
+ {name: "单个空格", query: " ", expectedFTS: `" "`},
+ {name: "连续空格", query: " ", expectedFTS: `" "`},
+ {name: "制表符", query: "\t", expectedFTS: "\"\t\""},
+ {name: "全角空格", query: "\u3000", expectedFTS: "\"\u3000\""},
+ {name: "普通关键词", query: "Parent", expectedFTS: `"Parent"`, expectedHPath: "Parent"},
+ {name: "首尾空格", query: " Parent ", expectedFTS: `" Parent "`, expectedHPath: "Parent"},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ ftsQuery, hPathQuery := buildKeywordSearchQueries(test.query)
+ if test.expectedFTS != ftsQuery {
+ t.Fatalf("FTS 查询错误:got %q, want %q", ftsQuery, test.expectedFTS)
+ }
+ if test.expectedHPath != hPathQuery {
+ t.Fatalf("层级路径查询错误:got %q, want %q", hPathQuery, test.expectedHPath)
+ }
+ })
+ }
+}
+
func TestBuildExactAliasSearchOrderCondition(t *testing.T) {
tests := []struct {
name string