Skip to content

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

:art: Improve hierarchical path search highlighting https://github.com/siyuan-note/siyuan/issues/18305

Daniele5fc0c50ae21
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 8616cc7733..a097703223 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -2429,19 +2429,13 @@ func loadFTSAndHPathMatchBlocks(matches []ftsAndHPathMatch, rawQuery, query stri
}
for _, match := range matches {
sqlBlock := ftsBlocks[match.rowID]
- terms := ""
if 0 != match.source {
sqlBlock = hPathBlocks[match.rowID]
- terms = rawQuery
}
if nil == sqlBlock {
continue
}
- block := fromSQLBlock(sqlBlock, terms, beforeLen)
- if 0 == match.source {
- markBlockHPath(block, sqlBlock.HPath, rawQuery, -1)
- }
- ret = append(ret, block)
+ ret = append(ret, fromHPathSearchSQLBlock(sqlBlock, rawQuery, beforeLen))
}
return
}
@@ -2681,7 +2675,13 @@ func fullTextSearchByLikeWithRootInBox(query, boxFilter, pathFilter string, boxA
keywords = gulu.Str.RemoveDuplicatedElem(keywords)
terms := strings.Join(keywords, search.TermSep)
terms = strings.ReplaceAll(terms, "''", "'")
- ret = fromSQLBlocks(&resultBlocks, terms, beforeLen)
+ for i, resultBlock := range resultBlocks {
+ if 1 == result[i]["matchSource"].(int64) {
+ ret = append(ret, fromHPathSearchSQLBlock(resultBlock, terms, beforeLen))
+ } else {
+ ret = append(ret, fromSQLBlock(resultBlock, terms, beforeLen))
+ }
+ }
if 1 > len(ret) {
ret = []*Block{}
}
@@ -2975,6 +2975,12 @@ func markBlockHPath(block *Block, hPath, terms string, beforeLen int) {
block.HPath = hPath
}
+func fromHPathSearchSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Block) {
+ block = fromSQLBlock(sqlBlock, "", beforeLen)
+ markBlockHPath(block, sqlBlock.HPath, terms, -1)
+ return
+}
+
func maxContent(content string, maxLen int) string {
idx := strings.Index(content, "<mark>")
if 128 < maxLen && maxLen <= idx {
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index fb040356c3..b72373af58 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -25,6 +25,7 @@ import (
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -371,6 +372,39 @@ func TestBuildHPathContainsCondition(t *testing.T) {
}
}
+func TestFromHPathSearchSQLBlockOnlyMarksHPath(t *testing.T) {
+ setSearchCaseSensitive(t, true)
+ sqlBlock := &sql.Block{
+ ID: "20260730160000-hpath01",
+ RootID: "20260730160000-hpath01",
+ HPath: "/思源笔记/子文档",
+ Name: "引用思源笔记",
+ Alias: "思源笔记",
+ Memo: "思源笔记",
+ Tag: "思源笔记",
+ Content: "((20260730160001-ref0001 '思源笔记'))",
+ Type: "d",
+ }
+
+ block := fromHPathSearchSQLBlock(sqlBlock, "思源笔记", 36)
+ if strings.Contains(block.Content, "<mark>") ||
+ strings.Contains(block.Name, "<mark>") ||
+ strings.Contains(block.Alias, "<mark>") ||
+ strings.Contains(block.Memo, "<mark>") ||
+ strings.Contains(block.Tag, "<mark>") {
+ t.Fatalf("路径辅助命中不应高亮正文或文档引用:%+v", block)
+ }
+ if !strings.Contains(block.HPath, "<mark>思源笔记</mark>") {
+ t.Fatalf("路径辅助命中应高亮层级路径:%q", block.HPath)
+ }
+
+ sqlBlock.Content = search.SearchMarkLeft + "思源笔记" + search.SearchMarkRight
+ block = fromHPathSearchSQLBlock(sqlBlock, "思源笔记", 36)
+ if !strings.Contains(block.Content, "<mark>思源笔记</mark>") {
+ t.Fatalf("FTS 摘要高亮应保留:%q", block.Content)
+ }
+}
+
func TestBuildHPathSearchOrderBy(t *testing.T) {
setSearchCaseSensitive(t, true)
assertOrderBySequence(t, buildHPathSearchOrderBy("Parent", 0),