mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 15:10:12 +01:00
✨ Support for searching asset content https://github.com/siyuan-note/siyuan/issues/8874
This commit is contained in:
parent
298b95dea3
commit
c96e815aa6
3 changed files with 84 additions and 21 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
// method:0:关键字,1:查询语法,2:SQL,3:正则表达式
|
||||
methodArg := arg["method"]
|
||||
if nil != methodArg {
|
||||
method = int(methodArg.(float64))
|
||||
}
|
||||
|
||||
// orderBy:0:相关度(默认),1:按更新时间升序,2:按更新时间降序
|
||||
orderByArg := arg["orderBy"]
|
||||
if nil != orderByArg {
|
||||
orderBy = int(orderByArg.(float64))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,24 +51,24 @@ type AssetContent struct {
|
|||
//
|
||||
// method:0:关键字,1:查询语法,2:SQL,3:正则表达式
|
||||
// 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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue