Skip to content

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

:art: Improve the SQL method in the global search https://github.com/siyuan-note/siyuan/issues/14641

Daniela5eab977289f
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 33b97e5ce0..686c85300c 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -1406,19 +1406,34 @@ func searchBySQL(stmt string, beforeLen, page, pageSize int) (ret []*Block, matc
}
stmt = strings.ToLower(stmt)
- if strings.HasPrefix(stmt, "select a.* ") { // 多个搜索关键字匹配文档 https://github.com/siyuan-note/siyuan/issues/7350
- stmt = strings.ReplaceAll(stmt, "select a.* ", "select COUNT(a.id) AS `matches`, COUNT(DISTINCT(a.root_id)) AS `docs` ")
- } else {
- stmt = strings.ReplaceAll(stmt, "select * ", "select COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` ")
+ stdQuery := !strings.Contains(stmt, "with recursive") && !strings.Contains(stmt, "union")
+ if stdQuery {
+ if strings.HasPrefix(stmt, "select a.* ") { // 多个搜索关键字匹配文档 https://github.com/siyuan-note/siyuan/issues/7350
+ stmt = strings.ReplaceAll(stmt, "select a.* ", "select COUNT(a.id) AS `matches`, COUNT(DISTINCT(a.root_id)) AS `docs` ")
+ } else {
+ stmt = strings.ReplaceAll(stmt, "select * ", "select COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` ")
+ }
}
stmt = removeLimitClause(stmt)
result, _ := sql.QueryNoLimit(stmt)
- if 1 > len(ret) {
+ if 1 > len(result) {
return
}
- matchedBlockCount = int(result[0]["matches"].(int64))
- matchedRootCount = int(result[0]["docs"].(int64))
+ if !stdQuery {
+ var rootIDs, blockIDs []string
+ for _, queryResult := range result {
+ rootIDs = append(rootIDs, queryResult["root_id"].(string))
+ blockIDs = append(blockIDs, queryResult["id"].(string))
+ }
+ rootIDs = gulu.Str.RemoveDuplicatedElem(rootIDs)
+ blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
+ matchedRootCount = len(rootIDs)
+ matchedBlockCount = len(blockIDs)
+ } else {
+ matchedBlockCount = int(result[0]["matches"].(int64))
+ matchedRootCount = int(result[0]["docs"].(int64))
+ }
return
}