Skip to content

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

:art: Global search supports pagination to display results https://github.com/siyuan-note/siyuan/issues/7948

Liang Ding1eba13162492
diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go
index 23002b0119..b61a099a35 100644
--- a/kernel/model/backlink.go
+++ b/kernel/model/backlink.go
@@ -668,7 +668,7 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink
buf.WriteString(" ORDER BY id DESC LIMIT " + strconv.Itoa(Conf.Search.Limit))
query := buf.String()
- sqlBlocks := sql.SelectBlocksRawStmt(query, Conf.Search.Limit)
+ sqlBlocks := sql.SelectBlocksRawStmt(query, 1, Conf.Search.Limit)
terms := mentionKeywords
if "" != keyword {
terms = append(terms, keyword)
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 8509e02668..11b7a092e0 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -1862,7 +1862,7 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, rootID string, blockR
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
stmt = html.UnescapeString(stmt)
stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
- sqlBlocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
+ sqlBlocks := sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
for _, b := range sqlBlocks {
subNodes := renderBlockMarkdownR0(b.ID, &rendered)
for _, subNode := range subNodes {
diff --git a/kernel/model/render.go b/kernel/model/render.go
index 41542f34c1..d6e8a489dd 100644
--- a/kernel/model/render.go
+++ b/kernel/model/render.go
@@ -219,7 +219,7 @@ func renderBlockMarkdownR0(id string, rendered *[]string) (ret []*ast.Node) {
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
stmt = html.UnescapeString(stmt)
stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
- sqlBlocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
+ sqlBlocks := sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
for _, sqlBlock := range sqlBlocks {
subNodes := renderBlockMarkdownR0(sqlBlock.ID, rendered)
for _, subNode := range subNodes {
diff --git a/kernel/model/search.go b/kernel/model/search.go
index c9d32ebf83..87a0e38ec4 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -409,7 +409,7 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
pathFilter := buildPathsFilter(paths)
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, filter, orderByClause, beforeLen, page)
case 2: // SQL
- blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen)
+ blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen, page)
case 3: // 正则表达式
typeFilter := buildTypeFilter(types)
boxFilter := buildBoxesFilter(boxes)
@@ -600,10 +600,10 @@ func buildTypeFilter(types map[string]bool) string {
return s.TypeFilter()
}
-func searchBySQL(stmt string, beforeLen int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
+func searchBySQL(stmt string, beforeLen, page int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
stmt = gulu.Str.RemoveInvisible(stmt)
stmt = strings.TrimSpace(stmt)
- blocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
+ blocks := sql.SelectBlocksRawStmt(stmt, page, Conf.Search.Limit)
ret = fromSQLBlocks(&blocks, "", beforeLen)
if 1 > len(ret) {
ret = []*Block{}
@@ -630,7 +630,7 @@ func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret []
keyword = gulu.Str.RemoveInvisible(keyword)
if ast.IsNodeIDPattern(keyword) {
- ret, _, _ = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+keyword+"'", 36)
+ ret, _, _ = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+keyword+"'", 36, 1)
return
}
@@ -682,7 +682,7 @@ func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret []
func fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
query = gulu.Str.RemoveInvisible(query)
if ast.IsNodeIDPattern(query) {
- ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen)
+ ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page)
return
}
return fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy, beforeLen, page)
@@ -691,7 +691,7 @@ func fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, order
func fullTextSearchByKeyword(query, boxFilter, pathFilter, typeFilter string, orderBy string, beforeLen, page int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
query = gulu.Str.RemoveInvisible(query)
if ast.IsNodeIDPattern(query) {
- ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen)
+ ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page)
return
}
query = stringQuery(query)
@@ -747,7 +747,7 @@ func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy strin
stmt += boxFilter + pathFilter
stmt += " " + orderBy
stmt += " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize)
- blocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
+ blocks := sql.SelectBlocksRawStmt(stmt, page, Conf.Search.Limit)
ret = fromSQLBlocks(&blocks, "", beforeLen)
if 1 > len(ret) {
ret = []*Block{}
diff --git a/kernel/model/template.go b/kernel/model/template.go
index fb3d348b19..f3cc995fc8 100644
--- a/kernel/model/template.go
+++ b/kernel/model/template.go
@@ -196,7 +196,7 @@ func renderTemplate(p, id string) (string, error) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
}
- ret = sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
+ ret = sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
return
}
funcMap["querySpans"] = func(stmt string, args ...string) (ret []*sql.Span) {
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index d324507806..11dda29a8e 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -417,7 +417,7 @@ func SelectBlocksRawStmtNoParse(stmt string, limit int) (ret []*Block) {
return selectBlocksRawStmt(stmt, limit)
}
-func SelectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
+func SelectBlocksRawStmt(stmt string, page, limit int) (ret []*Block) {
parsedStmt, err := sqlparser.Parse(stmt)
if nil != err {
return selectBlocksRawStmt(stmt, limit)
@@ -433,6 +433,15 @@ func SelectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
Val: []byte(strconv.Itoa(limit)),
},
}
+ } else {
+ slct.Limit.Rowcount = &sqlparser.SQLVal{
+ Type: sqlparser.IntVal,
+ Val: []byte(strconv.Itoa(limit)),
+ }
+ slct.Limit.Offset = &sqlparser.SQLVal{
+ Type: sqlparser.IntVal,
+ Val: []byte(strconv.Itoa((page - 1) * 32)),
+ }
}
stmt = sqlparser.String(slct)