Skip to content

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

:art: 搜索支持按文档分组和按文档树结构展现 https://github.com/siyuan-note/siyuan/issues/4772

Liang Dingfb14f7a46aed
diff --git a/kernel/api/search.go b/kernel/api/search.go
index cba275d0e5..5211bcd8ab 100644
--- a/kernel/api/search.go
+++ b/kernel/api/search.go
@@ -212,7 +212,12 @@ func fullTextSearchBlock(c *gin.Context) {
if nil != querySyntaxArg {
querySyntax = querySyntaxArg.(bool)
}
- blocks, matchedBlockCount, matchedRootCount := model.FullTextSearchBlock(query, box, path, types, querySyntax)
+ groupByArg := arg["groupBy"]
+ var groupBy int // 0:不分组,1:按文档分组,2:按文档树结构分组
+ if nil != groupByArg {
+ groupBy = int(groupByArg.(float64))
+ }
+ blocks, matchedBlockCount, matchedRootCount := model.FullTextSearchBlock(query, box, path, types, querySyntax, groupBy)
ret.Data = map[string]interface{}{
"blocks": blocks,
"matchedBlockCount": matchedBlockCount,
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 070ba0eace..ddd7347daa 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -253,13 +253,44 @@ func FindReplace(keyword, replacement string, ids []string) (err error) {
return
}
-func FullTextSearchBlock(query, box, path string, types map[string]bool, querySyntax bool) (ret []*Block, matchedBlockCount, matchedRootCount int) {
+func FullTextSearchBlock(query, box, path string, types map[string]bool, querySyntax bool, groupBy int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
query = strings.TrimSpace(query)
+ beforeLen := 36
+ var blocks []*Block
if queryStrLower := strings.ToLower(query); strings.Contains(queryStrLower, "select ") && strings.Contains(queryStrLower, " * ") && strings.Contains(queryStrLower, " from ") {
- ret, matchedBlockCount, matchedRootCount = searchBySQL(query, 36)
+ blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen)
} else {
filter := searchFilter(types)
- ret, matchedBlockCount, matchedRootCount = fullTextSearch(query, box, path, filter, 36, querySyntax)
+ blocks, matchedBlockCount, matchedRootCount = fullTextSearch(query, box, path, filter, beforeLen, querySyntax)
+ }
+
+ switch groupBy {
+ case 0: // 不分组
+ ret = blocks
+ case 1: // 按文档分组
+ rootMap := map[string]bool{}
+ var rootIDs []string
+ for _, b := range blocks {
+ if _, ok := rootMap[b.RootID]; !ok {
+ rootMap[b.RootID] = true
+ rootIDs = append(rootIDs, b.RootID)
+ }
+ }
+ sqlRoots := sql.GetBlocks(rootIDs)
+ roots := fromSQLBlocks(&sqlRoots, "", beforeLen)
+ for _, root := range roots {
+ for _, b := range blocks {
+ if b.RootID == root.ID {
+ root.Children = append(root.Children, b)
+ }
+ }
+ }
+ ret = roots
+ case 2: // 按文档树结构分组
+ // TODO
+ ret = blocks
+ default:
+ ret = blocks
}
return
}