From 1754dee724fa227e870c8ac1f84036f9928b50c8 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 13 Feb 2023 11:01:23 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20SQL=20=E6=90=9C=E7=B4=A2=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E4=B8=AA=E6=90=9C=E7=B4=A2=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=AD=97=E5=8C=B9=E9=85=8D=E6=96=87=E6=A1=A3=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7350?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/search.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/model/search.go b/kernel/model/search.go index 9d0b60ccb..ee6fa12b4 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -544,6 +544,7 @@ func buildTypeFilter(types map[string]bool) string { func searchBySQL(stmt string, beforeLen int) (ret []*Block, matchedBlockCount, matchedRootCount int) { stmt = gulu.Str.RemoveInvisible(stmt) + stmt = strings.TrimSpace(stmt) blocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit) ret = fromSQLBlocks(&blocks, "", beforeLen) if 1 > len(ret) { @@ -552,7 +553,11 @@ func searchBySQL(stmt string, beforeLen int) (ret []*Block, matchedBlockCount, m } stmt = strings.ToLower(stmt) - stmt = strings.ReplaceAll(stmt, "select * ", "select COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` ") + 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` ") + } result, _ := sql.Query(stmt) if 1 > len(ret) { return