Skip to content

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

:art: Apply case sensitivity to move destination search https://github.com/siyuan-note/siyuan/issues/18303

Daniel56498ff93d6a
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 07b4f08c06..8ab1538152 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -183,7 +183,7 @@ func SearchDocs(keyword string, flashcard bool, excludeIDs []string) (ret []map[
keywords := strings.Fields(keyword)
if 0 < len(keywords) {
for _, box := range boxes {
- if util.ContainsSubStr(box.Name, keywords) {
+ if containsSearchDocKeyword(box.Name, keywords, Conf.Search.CaseSensitive) {
data := map[string]string{"path": "/", "hPath": box.Name + "/", "box": box.ID, "boxIcon": box.Icon}
if flashcard {
newFlashcardCount, dueFlashcardCount, flashcardCount := countBoxFlashcard(box.ID, deck, deckBlockIDs)
@@ -194,7 +194,7 @@ func SearchDocs(keyword string, flashcard bool, excludeIDs []string) (ret []map[
data["dueFlashcardCount"] = strconv.Itoa(dueFlashcardCount)
data["flashcardCount"] = strconv.Itoa(flashcardCount)
}
- results = append(results, searchDocResult{data: data, exact: box.Name == keyword})
+ results = append(results, searchDocResult{data: data, exact: isExactSearchDocMatch(box.Name, keyword, Conf.Search.CaseSensitive)})
}
}
@@ -236,7 +236,7 @@ func SearchDocs(keyword string, flashcard bool, excludeIDs []string) (ret []map[
data["dueFlashcardCount"] = strconv.Itoa(dueFlashcardCount)
data["flashcardCount"] = strconv.Itoa(flashcardCount)
}
- results = append(results, searchDocResult{data: data, exact: rootBlock.Content == keyword})
+ results = append(results, searchDocResult{data: data, exact: isExactSearchDocMatch(rootBlock.Content, keyword, Conf.Search.CaseSensitive)})
}
sortSearchDocResults(results)
@@ -260,6 +260,28 @@ type searchDocResult struct {
exact bool
}
+func containsSearchDocKeyword(value string, keywords []string, caseSensitive bool) bool {
+ if !caseSensitive {
+ value = strings.ToLower(value)
+ }
+ for _, keyword := range keywords {
+ if !caseSensitive {
+ keyword = strings.ToLower(keyword)
+ }
+ if strings.Contains(value, keyword) {
+ return true
+ }
+ }
+ return false
+}
+
+func isExactSearchDocMatch(value, keyword string, caseSensitive bool) bool {
+ if caseSensitive {
+ return value == keyword
+ }
+ return strings.EqualFold(value, keyword)
+}
+
func buildSearchDocsCondition(keywords, excludeIDs []string, searchName, searchAlias, searchMemo bool) (condition string, args []any) {
var ret strings.Builder
for i, keyword := range keywords {
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
index 50cab7bb1f..7da4b70196 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -169,6 +169,47 @@ func TestSortSearchDocResults(t *testing.T) {
}
}
+func TestSearchDocTextMatching(t *testing.T) {
+ exactCases := []struct {
+ name string
+ value string
+ keyword string
+ caseSensitive bool
+ expected bool
+ }{
+ {name: "same case sensitive", value: "Math", keyword: "Math", caseSensitive: true, expected: true},
+ {name: "different case sensitive", value: "Math", keyword: "math", caseSensitive: true, expected: false},
+ {name: "different case insensitive", value: "Math", keyword: "math", caseSensitive: false, expected: true},
+ }
+ for _, test := range exactCases {
+ t.Run("exact/"+test.name, func(t *testing.T) {
+ if actual := isExactSearchDocMatch(test.value, test.keyword, test.caseSensitive); test.expected != actual {
+ t.Fatalf("unexpected exact match result: got %t, want %t", actual, test.expected)
+ }
+ })
+ }
+
+ containsCases := []struct {
+ name string
+ value string
+ keywords []string
+ caseSensitive bool
+ expected bool
+ }{
+ {name: "same case sensitive", value: "Math Notes", keywords: []string{"Math"}, caseSensitive: true, expected: true},
+ {name: "different case sensitive", value: "Math Notes", keywords: []string{"math"}, caseSensitive: true, expected: false},
+ {name: "different case insensitive", value: "Math Notes", keywords: []string{"math"}, caseSensitive: false, expected: true},
+ {name: "preserve any keyword matching", value: "Math Notes", keywords: []string{"missing", "notes"}, caseSensitive: false, expected: true},
+ }
+ for _, test := range containsCases {
+ t.Run("contains/"+test.name, func(t *testing.T) {
+ if actual := containsSearchDocKeyword(test.value, test.keywords, test.caseSensitive); test.expected != actual {
+ t.Fatalf("unexpected contains result: got %t, want %t", actual, test.expected)
+ }
+ })
+ }
+}
+
func TestBuildSearchDocsCondition(t *testing.T) {
condition, args := buildSearchDocsCondition([]string{"O'Reilly", "100%_done\\file"}, []string{"20260720000000-abc_def"}, true, true, true)
if strings.Contains(condition, "O'Reilly") || strings.Contains(condition, "100%_done") {
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index 0198335330..0ca3c94069 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -71,9 +71,10 @@ func queryBlockHashes(tx *sql.Tx, rootID string) (ret map[string]string) {
}
func QueryRootBlockByCondition(condition, exactKeyword string, limit int, args ...any) (ret []*Block) {
+ exactCondition, exactArg := rootBlockExactMatchCondition(exactKeyword, caseSensitive)
sqlStmt := "SELECT *, length(hpath) - length(replace(hpath, '/', '')) AS lv FROM blocks WHERE type = 'd' AND " + condition +
- " ORDER BY CASE WHEN content = ? THEN 0 ELSE 1 END ASC, box DESC, lv ASC LIMIT ?"
- args = append(args, exactKeyword, limit)
+ " ORDER BY CASE WHEN " + exactCondition + " THEN 0 ELSE 1 END ASC, box DESC, lv ASC LIMIT ?"
+ args = append(args, exactArg, limit)
rows, err := query(sqlStmt, args...)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
@@ -92,6 +93,13 @@ func QueryRootBlockByCondition(condition, exactKeyword string, limit int, args .
return
}
+func rootBlockExactMatchCondition(keyword string, sensitive bool) (condition, arg string) {
+ if sensitive {
+ return "content = ?", keyword
+ }
+ return "content LIKE ? ESCAPE '\\'", escapeLikePattern(keyword)
+}
+
func (block *Block) IsContainerBlock() bool {
return treenode.IsContainerType(block.Type)
}
diff --git a/kernel/sql/block_query_test.go b/kernel/sql/block_query_test.go
new file mode 100644
index 0000000000..03298a0a98
--- /dev/null
+++ b/kernel/sql/block_query_test.go
@@ -0,0 +1,31 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package sql
+
+import "testing"
+
+func TestRootBlockExactMatchCondition(t *testing.T) {
+ condition, arg := rootBlockExactMatchCondition("Math%_\\", true)
+ if "content = ?" != condition || "Math%_\\" != arg {
+ t.Fatalf("unexpected case-sensitive exact match: condition=%q arg=%q", condition, arg)
+ }
+
+ condition, arg = rootBlockExactMatchCondition("Math%_\\", false)
+ if "content LIKE ? ESCAPE '\\'" != condition || "Math\\%\\_\\\\" != arg {
+ t.Fatalf("unexpected case-insensitive exact match: condition=%q arg=%q", condition, arg)
+ }
+}