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

This commit is contained in:
Daniel 2025-04-19 17:01:34 +08:00
parent f60a8d641a
commit a5eab97728
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -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
}