Skip to content

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

:recycle: Consolidate full-text search options https://github.com/siyuan-note/siyuan/issues/20295

Danielc92ddcc6025c
diff --git a/kernel/api/search.go b/kernel/api/search.go
index baa206d839..b6f3da64f3 100644
--- a/kernel/api/search.go
+++ b/kernel/api/search.go
@@ -361,9 +361,41 @@ var fullTextSearchBlock = contractHandler(apicontract.FullTextSearchBlock, func(
if err := holdEncryptedBoxRequest(c, notebook); err != nil {
return apicontract.Failure[*apicontract.FullTextSearchBlockData](-1, err.Error())
}
- blocks, matchedBlockCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlockInBoxWithHPathContext(c.Request.Context(), query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, notebook, searchHPath, excludeBoxIDs, excludeDocIDs)
+ blocks, matchedBlockCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlock(model.BlockSearchOptions{
+ Context: c.Request.Context(),
+ Query: query,
+ Boxes: boxes,
+ Paths: paths,
+ Types: types,
+ SubTypes: subTypes,
+ Method: method,
+ OrderBy: orderBy,
+ GroupBy: groupBy,
+ Page: page,
+ PageSize: pageSize,
+ BoxID: notebook,
+ SearchHPath: new(searchHPath),
+ ExcludeBoxIDs: excludeBoxIDs,
+ ExcludeDocIDs: excludeDocIDs,
+ })
} else {
- blocks, matchedBlockCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlockInBoxWithHPathContext(c.Request.Context(), query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, "", searchHPath, excludeBoxIDs, excludeDocIDs)
+ blocks, matchedBlockCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlock(model.BlockSearchOptions{
+ Context: c.Request.Context(),
+ Query: query,
+ Boxes: boxes,
+ Paths: paths,
+ Types: types,
+ SubTypes: subTypes,
+ Method: method,
+ OrderBy: orderBy,
+ GroupBy: groupBy,
+ Page: page,
+ PageSize: pageSize,
+ BoxID: "",
+ SearchHPath: new(searchHPath),
+ ExcludeBoxIDs: excludeBoxIDs,
+ ExcludeDocIDs: excludeDocIDs,
+ })
}
if c.Request.Context().Err() != nil {
return apicontract.Success[*apicontract.FullTextSearchBlockData](nil)
diff --git a/kernel/cli/cmd/search.go b/kernel/cli/cmd/search.go
index 09d9468f55..26088b5d34 100644
--- a/kernel/cli/cmd/search.go
+++ b/kernel/cli/cmd/search.go
@@ -72,7 +72,18 @@ var searchCmd = &cobra.Command{
model.SemanticSearchBlock(query, notebooks, paths, types, subTypes, page, pageSize, nil, nil)
} else {
blocks, matchedBlockCount, matchedRootCount, pageCount, docMode =
- model.FullTextSearchBlock(query, notebooks, paths, types, subTypes, method, orderBy, groupBy, page, pageSize)
+ model.FullTextSearchBlock(model.BlockSearchOptions{
+ Query: query,
+ Boxes: notebooks,
+ Paths: paths,
+ Types: types,
+ SubTypes: subTypes,
+ Method: method,
+ OrderBy: orderBy,
+ GroupBy: groupBy,
+ Page: page,
+ PageSize: pageSize,
+ })
}
switch outputFormat {
diff --git a/kernel/mcp/tools/search.go b/kernel/mcp/tools/search.go
index 1b7d627273..3cfadf2d94 100644
--- a/kernel/mcp/tools/search.go
+++ b/kernel/mcp/tools/search.go
@@ -135,13 +135,32 @@ func fulltextSearch(args map[string]any) (CallToolResult, error) {
return sqlQuery(map[string]any{"stmt": query, "notebook": encryptedNotebook})
}
if encryptedNotebook != "" {
- blocks, matchedCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlockInBox(
- query, notebooks, paths, types, subtypes, method, orderBy, groupBy, page, pageSize, encryptedNotebook,
- )
+ blocks, matchedCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlock(model.BlockSearchOptions{
+ Query: query,
+ Boxes: notebooks,
+ Paths: paths,
+ Types: types,
+ SubTypes: subtypes,
+ Method: method,
+ OrderBy: orderBy,
+ GroupBy: groupBy,
+ Page: page,
+ PageSize: pageSize,
+ BoxID: encryptedNotebook,
+ })
} else {
- blocks, matchedCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlock(
- query, notebooks, paths, types, subtypes, method, orderBy, groupBy, page, pageSize,
- )
+ blocks, matchedCount, matchedRootCount, pageCount, docMode = model.FullTextSearchBlock(model.BlockSearchOptions{
+ Query: query,
+ Boxes: notebooks,
+ Paths: paths,
+ Types: types,
+ SubTypes: subtypes,
+ Method: method,
+ OrderBy: orderBy,
+ GroupBy: groupBy,
+ Page: page,
+ PageSize: pageSize,
+ })
}
if matchedCount == 0 {
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 36c7c8af87..2926f2dfe4 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -667,7 +667,20 @@ func FindReplaceInBox(keyword, replacement string, replaceTypes map[string]bool,
// `Replace All` is no longer affected by pagination https://github.com/siyuan-note/siyuan/issues/8265
// 替换目标始终使用未分组的块级结果,分组仅影响搜索结果展示
// https://github.com/siyuan-note/siyuan/issues/10825
- blocks, _, _, _, _ := FullTextSearchBlockInBoxWithHPath(keyword, boxes, paths, types, subTypes, method, 0, 0, 1, math.MaxInt, boxID, false)
+ blocks, _, _, _, _ := FullTextSearchBlock(BlockSearchOptions{
+ Query: keyword,
+ Boxes: boxes,
+ Paths: paths,
+ Types: types,
+ SubTypes: subTypes,
+ Method: method,
+ OrderBy: 0,
+ GroupBy: 0,
+ Page: 1,
+ PageSize: math.MaxInt,
+ BoxID: boxID,
+ SearchHPath: new(false),
+ })
for _, block := range blocks {
ids = append(ids, block.ID)
}
@@ -1575,32 +1588,48 @@ func mergeSamePreNext(n *ast.Node) {
}
}
-// FullTextSearchBlock 搜索内容块。
-//
-// method:0:关键字,1:查询语法,2:SQL,3:正则表达式
-// orderBy: 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时),6:按相关度升序,7:按相关度降序
-// groupBy:0:不分组,1:按文档分组
-func FullTextSearchBlock(query string, boxes, paths []string, types, subTypes map[string]bool, method, orderBy, groupBy, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
- return FullTextSearchBlockWithHPath(query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, true)
-}
-
-// FullTextSearchBlockWithHPath 搜索内容块,并可控制是否搜索文档层级路径。
-func FullTextSearchBlockWithHPath(query string, boxes, paths []string, types, subTypes map[string]bool, method, orderBy, groupBy, page, pageSize int, searchHPath bool) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
- return FullTextSearchBlockInBoxWithHPath(query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, "", searchHPath)
-}
-
-// FullTextSearchBlockInBox 与 FullTextSearchBlock 一致,但按 boxID 路由到加密 db 或全局 db。
-// 加密笔记本内搜索时传入 boxID,所有 sql/treenode 查询走加密 db;boxID 为空时 fall-through 全局 db。
-func FullTextSearchBlockInBox(query string, boxes, paths []string, types, subTypes map[string]bool, method, orderBy, groupBy, page, pageSize int, boxID string) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
- return FullTextSearchBlockInBoxWithHPath(query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, boxID, true)
-}
-
-// FullTextSearchBlockInBoxWithHPath 与 FullTextSearchBlockInBox 一致,并可控制是否搜索文档层级路径。
-func FullTextSearchBlockInBoxWithHPath(query string, boxes, paths []string, types, subTypes map[string]bool, method, orderBy, groupBy, page, pageSize int, boxID string, searchHPath bool) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
- return FullTextSearchBlockInBoxWithHPathContext(context.Background(), query, boxes, paths, types, subTypes, method, orderBy, groupBy, page, pageSize, boxID, searchHPath, nil, nil)
-}
+// BlockSearchOptions 汇总内容块搜索条件;未指定上下文时使用后台上下文,SearchHPath 缺省为 true。
+// BoxID 决定普通或加密笔记本的数据库路由,访问排除条件仍由调用方按请求权限提供。
+type BlockSearchOptions struct {
+ Context context.Context
+ Query string
+ Boxes []string
+ Paths []string
+ Types map[string]bool
+ SubTypes map[string]bool
+ Method int
+ OrderBy int
+ GroupBy int
+ Page int
+ PageSize int
+ BoxID string
+ SearchHPath *bool
+ ExcludeBoxIDs []string
+ ExcludeDocIDs []string
+}
+
+// FullTextSearchBlock 按同一组命名条件搜索内容块。
+// Method:0 为关键字,1 为查询语法,2 为 SQL,3 为正则表达式;GroupBy:0 不分组,1 按文档分组。
+// OrderBy:0 按块类型,1/2 按创建时间升/降序,3/4 按更新时间升/降序,5 按文档内内容顺序,6/7 按相关度升/降序。
+func FullTextSearchBlock(options BlockSearchOptions) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
+ ctx := options.Context
+ if ctx == nil {
+ ctx = context.Background()
+ }
+ query := options.Query
+ boxes := options.Boxes
+ paths := options.Paths
+ types := options.Types
+ subTypes := options.SubTypes
+ method := options.Method
+ orderBy := options.OrderBy
+ groupBy := options.GroupBy
+ page := options.Page
+ pageSize := options.PageSize
+ boxID := options.BoxID
+ searchHPath := options.SearchHPath == nil || *options.SearchHPath
+ excludeBoxIDs, excludeDocIDs := options.ExcludeBoxIDs, options.ExcludeDocIDs
-func FullTextSearchBlockInBoxWithHPathContext(ctx context.Context, query string, boxes, paths []string, types, subTypes map[string]bool, method, orderBy, groupBy, page, pageSize int, boxID string, searchHPath bool, excludeBoxIDs, excludeDocIDs []string) (ret []*Block, matchedBlockCount, matchedRootCount, pageCount int, docMode bool) {
ret = []*Block{}
if "" == query {
return