Skip to content

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

:art: Improve exact name and alias matching in document move search https://github.com/siyuan-note/siyuan/issues/19317

Daniel2fce8b9d3e10
diff --git a/kernel/model/file.go b/kernel/model/file.go
index a9f9f62dee..8dbec4ebb4 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -312,7 +312,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: isExactSearchDocMatch(rootBlock.Content, keyword, Conf.Search.CaseSensitive)})
+ results = append(results, searchDocResult{data: data, exact: isExactSearchDocBlockMatch(rootBlock, keyword, Conf.Search.CaseSensitive)})
}
sortSearchDocResults(results)
@@ -358,6 +358,20 @@ func isExactSearchDocMatch(value, keyword string, caseSensitive bool) bool {
return strings.EqualFold(value, keyword)
}
+func isExactSearchDocBlockMatch(block *sql.Block, keyword string, caseSensitive bool) bool {
+ if isExactSearchDocMatch(block.Content, keyword, caseSensitive) || isExactSearchDocMatch(block.Name, keyword, caseSensitive) {
+ return true
+ }
+ if "" != keyword && !strings.Contains(keyword, ",") {
+ for alias := range strings.SplitSeq(block.Alias, ",") {
+ if isExactSearchDocMatch(alias, keyword, caseSensitive) {
+ return true
+ }
+ }
+ }
+ return false
+}
+
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 582d9046ac..b16bab0dfc 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -29,6 +29,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -1147,6 +1148,32 @@ func TestSortSearchDocResults(t *testing.T) {
}
}
+func TestSearchDocBlockExactMatching(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ block sql.Block
+ keyword string
+ sensitive bool
+ want bool
+ }{
+ {"title", sql.Block{Content: "Math"}, "Math", true, true},
+ {"name", sql.Block{Name: "Math"}, "Math", true, true},
+ {"alias", sql.Block{Alias: "Algebra,Math,Geometry"}, "Math", true, true},
+ {"partial alias", sql.Block{Alias: "Higher Math"}, "Math", true, false},
+ {"multiple aliases", sql.Block{Alias: "Math,Algebra"}, "Math,Algebra", true, false},
+ {"sensitive", sql.Block{Name: "Math", Alias: "Math"}, "math", true, false},
+ {"insensitive name", sql.Block{Name: "Math"}, "math", false, true},
+ {"insensitive alias", sql.Block{Alias: "Algebra,Math"}, "math", false, true},
+ {"literal", sql.Block{Alias: "Other,Math%_\\"}, "Math%_\\", true, true},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ if got := isExactSearchDocBlockMatch(&test.block, test.keyword, test.sensitive); got != test.want {
+ t.Fatalf("got %t, want %t", got, test.want)
+ }
+ })
+ }
+}
+
func TestSearchDocTextMatching(t *testing.T) {
exactCases := []struct {
name string
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index b1c40efd3d..3f54f74cb2 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -91,7 +91,8 @@ func QueryRootBlockByCondition(condition, exactKeyword string, limit int, args .
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 " + exactCondition + " THEN 0 ELSE 1 END ASC, box DESC, lv ASC LIMIT ?"
- args = append(args, exactArg, limit)
+ args = append(args, exactArg...)
+ args = append(args, limit)
rows, err := query(sqlStmt, args...)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
@@ -114,7 +115,8 @@ func QueryRootBlockByConditionInBox(condition, exactKeyword string, limit int, b
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 " + exactCondition + " THEN 0 ELSE 1 END ASC, box DESC, lv ASC LIMIT ?"
- args = append(args, exactArg, limit)
+ args = append(args, exactArg...)
+ args = append(args, limit)
rows, err := queryForBox(boxID, sqlStmt, args...)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
@@ -133,11 +135,24 @@ func QueryRootBlockByConditionInBox(condition, exactKeyword string, limit int, b
return
}
-func rootBlockExactMatchCondition(keyword string, sensitive bool) (condition, arg string) {
+func rootBlockExactMatchCondition(keyword string, sensitive bool) (condition string, args []any) {
if sensitive {
- return "content = ?", keyword
+ condition = "content = ? OR name = ?"
+ args = []any{keyword, keyword}
+ } else {
+ condition = "content LIKE ? ESCAPE '\\' OR name LIKE ? ESCAPE '\\'"
+ args = []any{escapeLikePattern(keyword), escapeLikePattern(keyword)}
}
- return "content LIKE ? ESCAPE '\\'", escapeLikePattern(keyword)
+ if "" != keyword && !strings.Contains(keyword, ",") {
+ if sensitive {
+ condition += " OR instr(',' || alias || ',', ?) > 0"
+ args = append(args, ","+keyword+",")
+ } else {
+ condition += " OR (',' || alias || ',') LIKE ? ESCAPE '\\'"
+ args = append(args, "%,"+escapeLikePattern(keyword)+",%")
+ }
+ }
+ return
}
func (block *Block) IsContainerBlock() bool {
diff --git a/kernel/sql/block_query_test.go b/kernel/sql/block_query_test.go
index ffa60eb1a7..935fdef27d 100644
--- a/kernel/sql/block_query_test.go
+++ b/kernel/sql/block_query_test.go
@@ -18,6 +18,7 @@ package sql
import (
gosql "database/sql"
+ "reflect"
"testing"
)
@@ -102,12 +103,12 @@ func TestGraphChildBlocksAcceptNormalCondition(t *testing.T) {
func TestRootBlockExactMatchCondition(t *testing.T) {
condition, arg := rootBlockExactMatchCondition("Math%_\\", true)
- if "content = ?" != condition || "Math%_\\" != arg {
+ if "content = ? OR name = ? OR instr(',' || alias || ',', ?) > 0" != condition || !reflect.DeepEqual(arg, []any{"Math%_\\", "Math%_\\", ",Math%_\\,"}) {
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 {
+ if "content LIKE ? ESCAPE '\\' OR name LIKE ? ESCAPE '\\' OR (',' || alias || ',') LIKE ? ESCAPE '\\'" != condition || !reflect.DeepEqual(arg, []any{"Math\\%\\_\\\\", "Math\\%\\_\\\\", "%,Math\\%\\_\\\\,%"}) {
t.Fatalf("unexpected case-insensitive exact match: condition=%q arg=%q", condition, arg)
}
}
@@ -148,6 +149,36 @@ func TestQueryLikeEscape(t *testing.T) {
}
}
+func TestRootBlockExactMatchBeforeLimit(t *testing.T) {
+ testDB := createGraphTestBlocksTable(t)
+ for _, sensitive := range []bool{true, false} {
+ for _, field := range []string{"content", "name", "alias"} {
+ if _, err := testDB.Exec("DELETE FROM blocks"); err != nil {
+ t.Fatal(err)
+ }
+ keyword := "Math%_\\'"
+ value := keyword
+ if field == "alias" {
+ value = "Other," + value + ",Last"
+ }
+ if _, err := testDB.Exec("INSERT INTO blocks (id, content, name, alias) VALUES ('partial', 'Higher Math', 'Higher Math', 'Higher Math')"); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := testDB.Exec("INSERT INTO blocks (id, "+field+") VALUES ('exact', ?)", value); err != nil {
+ t.Fatal(err)
+ }
+ condition, args := rootBlockExactMatchCondition(keyword, sensitive)
+ var id string
+ if err := testDB.QueryRow("SELECT id FROM blocks ORDER BY CASE WHEN "+condition+" THEN 0 ELSE 1 END, rowid LIMIT 1", args...).Scan(&id); err != nil {
+ t.Fatal(err)
+ }
+ if id != "exact" {
+ t.Fatalf("exact %s match lost before limit (sensitive=%t): %s", field, sensitive, id)
+ }
+ }
+ }
+}
+
func TestQueryPreservesOriginalErrorWithLimit(t *testing.T) {
testDB, err := gosql.Open("sqlite3_extended", ":memory:")
if err != nil {