From fb14f7a46aed7a6cd465f6f7b1fc857743f4c611 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 26 Nov 2022 18:12:54 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:art:=20=E6=90=9C=E7=B4=A2=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=89=E6=96=87=E6=A1=A3=E5=88=86=E7=BB=84=E5=92=8C?= =?UTF-8?q?=E6=8C=89=E6=96=87=E6=A1=A3=E6=A0=91=E7=BB=93=E6=9E=84=E5=B1=95?= =?UTF-8?q?=E7=8E=B0=20https://github.com/siyuan-note/siyuan/issues/4772?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/search.go | 7 ++++++- kernel/model/search.go | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/kernel/api/search.go b/kernel/api/search.go index cba275d0e..5211bcd8a 100644 --- a/kernel/api/search.go +++ b/kernel/api/search.go @@ -212,7 +212,12 @@ func fullTextSearchBlock(c *gin.Context) { if nil != querySyntaxArg { querySyntax = querySyntaxArg.(bool) } - blocks, matchedBlockCount, matchedRootCount := model.FullTextSearchBlock(query, box, path, types, querySyntax) + groupByArg := arg["groupBy"] + var groupBy int // 0:不分组,1:按文档分组,2:按文档树结构分组 + if nil != groupByArg { + groupBy = int(groupByArg.(float64)) + } + blocks, matchedBlockCount, matchedRootCount := model.FullTextSearchBlock(query, box, path, types, querySyntax, groupBy) ret.Data = map[string]interface{}{ "blocks": blocks, "matchedBlockCount": matchedBlockCount, diff --git a/kernel/model/search.go b/kernel/model/search.go index 070ba0eac..ddd7347da 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -253,13 +253,44 @@ func FindReplace(keyword, replacement string, ids []string) (err error) { return } -func FullTextSearchBlock(query, box, path string, types map[string]bool, querySyntax bool) (ret []*Block, matchedBlockCount, matchedRootCount int) { +func FullTextSearchBlock(query, box, path string, types map[string]bool, querySyntax bool, groupBy int) (ret []*Block, matchedBlockCount, matchedRootCount int) { query = strings.TrimSpace(query) + beforeLen := 36 + var blocks []*Block if queryStrLower := strings.ToLower(query); strings.Contains(queryStrLower, "select ") && strings.Contains(queryStrLower, " * ") && strings.Contains(queryStrLower, " from ") { - ret, matchedBlockCount, matchedRootCount = searchBySQL(query, 36) + blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen) } else { filter := searchFilter(types) - ret, matchedBlockCount, matchedRootCount = fullTextSearch(query, box, path, filter, 36, querySyntax) + blocks, matchedBlockCount, matchedRootCount = fullTextSearch(query, box, path, filter, beforeLen, querySyntax) + } + + switch groupBy { + case 0: // 不分组 + ret = blocks + case 1: // 按文档分组 + rootMap := map[string]bool{} + var rootIDs []string + for _, b := range blocks { + if _, ok := rootMap[b.RootID]; !ok { + rootMap[b.RootID] = true + rootIDs = append(rootIDs, b.RootID) + } + } + sqlRoots := sql.GetBlocks(rootIDs) + roots := fromSQLBlocks(&sqlRoots, "", beforeLen) + for _, root := range roots { + for _, b := range blocks { + if b.RootID == root.ID { + root.Children = append(root.Children, b) + } + } + } + ret = roots + case 2: // 按文档树结构分组 + // TODO + ret = blocks + default: + ret = blocks } return } From 2ce8ce79bd7a30d984ae3aad81f4756e1bf08e0d Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 26 Nov 2022 22:19:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:zap:=20=E6=94=B9=E8=BF=9B=E5=8F=8D?= =?UTF-8?q?=E9=93=BE=E9=9D=A2=E6=9D=BF=E5=8A=A0=E8=BD=BD=E6=80=A7=E8=83=BD?= =?UTF-8?q?=20Fix=20https://github.com/siyuan-note/siyuan/issues/6724?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/sql/block_query.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go index 7f3356e28..a1a4b1214 100644 --- a/kernel/sql/block_query.go +++ b/kernel/sql/block_query.go @@ -598,11 +598,29 @@ func GetAllRootBlocks() (ret []*Block) { } func GetBlocks(ids []string) (ret []*Block) { - length := len(ids) + var notHitIDs []string + cached := map[string]*Block{} + for _, id := range ids { + b := getBlockCache(id) + if nil != b { + cached[id] = b + } else { + notHitIDs = append(notHitIDs, id) + } + } + + if 1 > len(notHitIDs) { + for _, id := range ids { + ret = append(ret, cached[id]) + } + return + } + + length := len(notHitIDs) stmtBuilder := bytes.Buffer{} stmtBuilder.WriteString("SELECT * FROM blocks WHERE id IN (") var args []interface{} - for i, id := range ids { + for i, id := range notHitIDs { args = append(args, id) stmtBuilder.WriteByte('?') if i < length-1 { @@ -619,10 +637,13 @@ func GetBlocks(ids []string) (ret []*Block) { defer rows.Close() for rows.Next() { if block := scanBlockRows(rows); nil != block { - ret = append(ret, block) putBlockCache(block) + cached[block.ID] = block } } + for _, id := range ids { + ret = append(ret, cached[id]) + } return } From f678a8877e24144e0c3f802f1c06655c2a0bda6f Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 26 Nov 2022 22:34:28 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:art:=20=E6=90=9C=E7=B4=A2=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=89=E6=96=87=E6=A1=A3=E5=88=86=E7=BB=84=E5=B1=95?= =?UTF-8?q?=E7=8E=B0=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/4772?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/search.go | 2 +- kernel/model/search.go | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/kernel/api/search.go b/kernel/api/search.go index 5211bcd8a..385dd303e 100644 --- a/kernel/api/search.go +++ b/kernel/api/search.go @@ -213,7 +213,7 @@ func fullTextSearchBlock(c *gin.Context) { querySyntax = querySyntaxArg.(bool) } groupByArg := arg["groupBy"] - var groupBy int // 0:不分组,1:按文档分组,2:按文档树结构分组 + var groupBy int // 0:不分组,1:按文档分组 if nil != groupByArg { groupBy = int(groupByArg.(float64)) } diff --git a/kernel/model/search.go b/kernel/model/search.go index ddd7347da..b24dc5ee7 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -286,9 +286,6 @@ func FullTextSearchBlock(query, box, path string, types map[string]bool, querySy } } ret = roots - case 2: // 按文档树结构分组 - // TODO - ret = blocks default: ret = blocks }