This commit is contained in:
Daniel 2023-08-11 10:28:32 +08:00
parent 298b95dea3
commit c96e815aa6
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 84 additions and 21 deletions

View file

@ -144,6 +144,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/search/fullTextSearchBlock", model.CheckAuth, fullTextSearchBlock)
ginServer.Handle("POST", "/api/search/searchAsset", model.CheckAuth, searchAsset)
ginServer.Handle("POST", "/api/search/findReplace", model.CheckAuth, model.CheckReadonly, findReplace)
ginServer.Handle("POST", "/api/search/fullTextSearchAssetContent", model.CheckAuth, fullTextSearchAssetContent)
ginServer.Handle("POST", "/api/block/getBlockInfo", model.CheckAuth, getBlockInfo)
ginServer.Handle("POST", "/api/block/getBlockDOM", model.CheckAuth, getBlockDOM)

View file

@ -26,6 +26,24 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func fullTextSearchAssetContent(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
page, pageSize, query, types, method, orderBy := parseSearchAssetContentArgs(arg)
assetContents, matchedAssetCount, pageCount := model.FullTextSearchAssetContent(query, types, method, orderBy, page, pageSize)
ret.Data = map[string]interface{}{
"assetContents": assetContents,
"matchedAssetCount": matchedAssetCount,
"pageCount": pageCount,
}
}
func findReplace(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
@ -35,7 +53,7 @@ func findReplace(c *gin.Context) {
return
}
_, _, _, paths, boxes, types, method, orderBy, groupBy := parseSearchArgs(arg)
_, _, _, paths, boxes, types, method, orderBy, groupBy := parseSearchBlockArgs(arg)
k := arg["k"].(string)
r := arg["r"].(string)
@ -215,7 +233,7 @@ func fullTextSearchBlock(c *gin.Context) {
return
}
page, pageSize, query, paths, boxes, types, method, orderBy, groupBy := parseSearchArgs(arg)
page, pageSize, query, paths, boxes, types, method, orderBy, groupBy := parseSearchBlockArgs(arg)
blocks, matchedBlockCount, matchedRootCount, pageCount := model.FullTextSearchBlock(query, boxes, paths, types, method, orderBy, groupBy, page, pageSize)
ret.Data = map[string]interface{}{
"blocks": blocks,
@ -225,7 +243,7 @@ func fullTextSearchBlock(c *gin.Context) {
}
}
func parseSearchArgs(arg map[string]interface{}) (page, pageSize int, query string, paths, boxes []string, types map[string]bool, method, orderBy, groupBy int) {
func parseSearchBlockArgs(arg map[string]interface{}) (page, pageSize int, query string, paths, boxes []string, types map[string]bool, method, orderBy, groupBy int) {
page = 1
if nil != arg["page"] {
page = int(arg["page"].(float64))
@ -291,3 +309,47 @@ func parseSearchArgs(arg map[string]interface{}) (page, pageSize int, query stri
}
return
}
func parseSearchAssetContentArgs(arg map[string]interface{}) (page, pageSize int, query string, types map[string]bool, method, orderBy int) {
page = 1
if nil != arg["page"] {
page = int(arg["page"].(float64))
}
if 0 >= page {
page = 1
}
pageSize = 32
if nil != arg["pageSize"] {
pageSize = int(arg["pageSize"].(float64))
}
if 0 >= pageSize {
pageSize = 32
}
queryArg := arg["query"]
if nil != queryArg {
query = queryArg.(string)
}
if nil != arg["types"] {
typesArg := arg["types"].(map[string]interface{})
types = map[string]bool{}
for t, b := range typesArg {
types[t] = b.(bool)
}
}
// method0关键字1查询语法2SQL3正则表达式
methodArg := arg["method"]
if nil != methodArg {
method = int(methodArg.(float64))
}
// orderBy0相关度默认1按更新时间升序2按更新时间降序
orderByArg := arg["orderBy"]
if nil != orderByArg {
orderBy = int(orderByArg.(float64))
}
return
}

View file

@ -51,24 +51,24 @@ type AssetContent struct {
//
// method0关键字1查询语法2SQL3正则表达式
// orderBy: 0相关度默认1按更新时间升序2按更新时间降序
func FullTextSearchAssetContent(query string, types map[string]bool, method, orderBy, page, pageSize int) (ret []*AssetContent, matchedAssetsCount, pageCount int) {
func FullTextSearchAssetContent(query string, types map[string]bool, method, orderBy, page, pageSize int) (ret []*AssetContent, matchedAssetCount, pageCount int) {
query = strings.TrimSpace(query)
beforeLen := 36
orderByClause := buildAssetContentOrderBy(orderBy)
switch method {
case 1: // 查询语法
filter := buildAssetContentTypeFilter(types)
ret, matchedAssetsCount = fullTextSearchAssetContentByQuerySyntax(query, filter, orderByClause, beforeLen, page, pageSize)
ret, matchedAssetCount = fullTextSearchAssetContentByQuerySyntax(query, filter, orderByClause, beforeLen, page, pageSize)
case 2: // SQL
ret, matchedAssetsCount = searchAssetContentBySQL(query, beforeLen, page, pageSize)
ret, matchedAssetCount = searchAssetContentBySQL(query, beforeLen, page, pageSize)
case 3: // 正则表达式
typeFilter := buildAssetContentTypeFilter(types)
ret, matchedAssetsCount = fullTextSearchAssetContentByRegexp(query, typeFilter, orderByClause, beforeLen, page, pageSize)
ret, matchedAssetCount = fullTextSearchAssetContentByRegexp(query, typeFilter, orderByClause, beforeLen, page, pageSize)
default: // 关键字
filter := buildAssetContentTypeFilter(types)
ret, matchedAssetsCount = fullTextSearchAssetContentByKeyword(query, filter, orderByClause, beforeLen, page, pageSize)
ret, matchedAssetCount = fullTextSearchAssetContentByKeyword(query, filter, orderByClause, beforeLen, page, pageSize)
}
pageCount = (matchedAssetsCount + pageSize - 1) / pageSize
pageCount = (matchedAssetCount + pageSize - 1) / pageSize
if 1 > len(ret) {
ret = []*AssetContent{}
@ -76,18 +76,18 @@ func FullTextSearchAssetContent(query string, types map[string]bool, method, ord
return
}
func fullTextSearchAssetContentByQuerySyntax(query, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetsCount int) {
func fullTextSearchAssetContentByQuerySyntax(query, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetCount int) {
query = gulu.Str.RemoveInvisible(query)
return fullTextSearchAssetContentByFTS(query, typeFilter, orderBy, beforeLen, page, pageSize)
}
func fullTextSearchAssetContentByKeyword(query, typeFilter string, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetsCount int) {
func fullTextSearchAssetContentByKeyword(query, typeFilter string, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetCount int) {
query = gulu.Str.RemoveInvisible(query)
query = stringQuery(query)
return fullTextSearchAssetContentByFTS(query, typeFilter, orderBy, beforeLen, page, pageSize)
}
func fullTextSearchAssetContentByRegexp(exp, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetsCount int) {
func fullTextSearchAssetContentByRegexp(exp, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetCount int) {
exp = gulu.Str.RemoveInvisible(exp)
fieldFilter := assetContentFieldRegexp(exp)
stmt := "SELECT * FROM `asset_contents_fts_case_insensitive` WHERE " + fieldFilter + " AND ext IN " + typeFilter
@ -99,7 +99,7 @@ func fullTextSearchAssetContentByRegexp(exp, typeFilter, orderBy string, beforeL
ret = []*AssetContent{}
}
matchedAssetsCount = fullTextSearchAssetContentCountByRegexp(exp, typeFilter)
matchedAssetCount = fullTextSearchAssetContentCountByRegexp(exp, typeFilter)
return
}
@ -113,7 +113,7 @@ func assetContentFieldRegexp(exp string) string {
return buf.String()
}
func fullTextSearchAssetContentCountByRegexp(exp, typeFilter string) (matchedAssetsCount int) {
func fullTextSearchAssetContentCountByRegexp(exp, typeFilter string) (matchedAssetCount int) {
table := "asset_contents_fts_case_insensitive"
fieldFilter := fieldRegexp(exp)
stmt := "SELECT COUNT(path) AS `assets` FROM `" + table + "` WHERE " + fieldFilter + " AND type IN " + typeFilter
@ -121,11 +121,11 @@ func fullTextSearchAssetContentCountByRegexp(exp, typeFilter string) (matchedAss
if 1 > len(result) {
return
}
matchedAssetsCount = int(result[0]["assets"].(int64))
matchedAssetCount = int(result[0]["assets"].(int64))
return
}
func fullTextSearchAssetContentByFTS(query, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetsCount int) {
func fullTextSearchAssetContentByFTS(query, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetCount int) {
table := "asset_contents_fts_case_insensitive"
projections := "id, name, ext, path, size, updated, " +
"highlight(" + table + ", 6, '<mark>', '</mark>') AS content"
@ -139,11 +139,11 @@ func fullTextSearchAssetContentByFTS(query, typeFilter, orderBy string, beforeLe
ret = []*AssetContent{}
}
matchedAssetsCount = fullTextSearchAssetContentCount(query, typeFilter)
matchedAssetCount = fullTextSearchAssetContentCount(query, typeFilter)
return
}
func searchAssetContentBySQL(stmt string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetsCount int) {
func searchAssetContentBySQL(stmt string, beforeLen, page, pageSize int) (ret []*AssetContent, matchedAssetCount int) {
stmt = gulu.Str.RemoveInvisible(stmt)
stmt = strings.TrimSpace(stmt)
assetContents := sql.SelectAssetContentsRawStmt(stmt, page, pageSize)
@ -161,11 +161,11 @@ func searchAssetContentBySQL(stmt string, beforeLen, page, pageSize int) (ret []
return
}
matchedAssetsCount = int(result[0]["assets"].(int64))
matchedAssetCount = int(result[0]["assets"].(int64))
return
}
func fullTextSearchAssetContentCount(query, typeFilter string) (matchedAssetsCount int) {
func fullTextSearchAssetContentCount(query, typeFilter string) (matchedAssetCount int) {
query = gulu.Str.RemoveInvisible(query)
table := "asset_contents_fts_case_insensitive"
@ -175,7 +175,7 @@ func fullTextSearchAssetContentCount(query, typeFilter string) (matchedAssetsCou
if 1 > len(result) {
return
}
matchedAssetsCount = int(result[0]["assets"].(int64))
matchedAssetCount = int(result[0]["assets"].(int64))
return
}