🎨 Return document blocks when search hits different block content https://github.com/siyuan-note/siyuan/issues/10584
This commit is contained in:
parent
3ba6dfed83
commit
da341269cd
1 changed files with 32 additions and 55 deletions
|
@ -914,27 +914,39 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
|
|||
query = trimQuery
|
||||
}
|
||||
|
||||
var ignoreFilter string
|
||||
if ignoreLines := getSearchIgnoreLines(); 0 < len(ignoreLines) {
|
||||
// Support ignore search results https://github.com/siyuan-note/siyuan/issues/10089
|
||||
buf := bytes.Buffer{}
|
||||
for _, line := range ignoreLines {
|
||||
buf.WriteString(" AND ")
|
||||
buf.WriteString(line)
|
||||
}
|
||||
ignoreFilter += buf.String()
|
||||
}
|
||||
|
||||
beforeLen := 36
|
||||
var blocks []*Block
|
||||
orderByClause := buildOrderBy(query, method, orderBy)
|
||||
switch method {
|
||||
case 1: // 查询语法
|
||||
filter := buildTypeFilter(types)
|
||||
typeFilter := buildTypeFilter(types)
|
||||
boxFilter := buildBoxesFilter(boxes)
|
||||
pathFilter := buildPathsFilter(paths)
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, filter, orderByClause, beforeLen, page, pageSize)
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderByClause, beforeLen, page, pageSize)
|
||||
case 2: // SQL
|
||||
blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen, page, pageSize)
|
||||
case 3: // 正则表达式
|
||||
typeFilter := buildTypeFilter(types)
|
||||
boxFilter := buildBoxesFilter(boxes)
|
||||
pathFilter := buildPathsFilter(paths)
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByRegexp(query, boxFilter, pathFilter, typeFilter, orderByClause, beforeLen, page, pageSize)
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByRegexp(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderByClause, beforeLen, page, pageSize)
|
||||
default: // 关键字
|
||||
filter := buildTypeFilter(types)
|
||||
typeFilter := buildTypeFilter(types)
|
||||
boxFilter := buildBoxesFilter(boxes)
|
||||
pathFilter := buildPathsFilter(paths)
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByKeyword(query, boxFilter, pathFilter, filter, orderByClause, beforeLen, page, pageSize)
|
||||
|
||||
blocks, matchedBlockCount, matchedRootCount = fullTextSearchByKeyword(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderByClause, beforeLen, page, pageSize)
|
||||
}
|
||||
pageCount = (matchedBlockCount + pageSize - 1) / pageSize
|
||||
|
||||
|
@ -1264,31 +1276,30 @@ func extractID(content string) (ret string) {
|
|||
return
|
||||
}
|
||||
|
||||
func fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
query = filterQueryInvisibleChars(query)
|
||||
if ast.IsNodeIDPattern(query) {
|
||||
ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize)
|
||||
return
|
||||
}
|
||||
return fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy, beforeLen, page, pageSize)
|
||||
return fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy, beforeLen, page, pageSize)
|
||||
}
|
||||
|
||||
func fullTextSearchByKeyword(query, boxFilter, pathFilter, typeFilter string, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchByKeyword(query, boxFilter, pathFilter, typeFilter, ignoreFilter string, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
query = filterQueryInvisibleChars(query)
|
||||
if ast.IsNodeIDPattern(query) {
|
||||
ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize)
|
||||
return
|
||||
}
|
||||
return fullTextSearchByFTSWithRoot(query, boxFilter, pathFilter, typeFilter, orderBy, beforeLen, page, pageSize)
|
||||
return fullTextSearchByFTSWithRoot(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy, beforeLen, page, pageSize)
|
||||
}
|
||||
|
||||
func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
exp = filterQueryInvisibleChars(exp)
|
||||
|
||||
fieldFilter := fieldRegexp(exp)
|
||||
stmt := "SELECT * FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter
|
||||
stmt += boxFilter + pathFilter
|
||||
stmt += " " + orderBy
|
||||
stmt += boxFilter + pathFilter + ignoreFilter + " " + orderBy
|
||||
regex := regexp.MustCompile(exp)
|
||||
blocks := sql.SelectBlocksRegex(stmt, regex, Conf.Search.Name, Conf.Search.Alias, Conf.Search.Memo, Conf.Search.IAL, page, pageSize)
|
||||
ret = fromSQLBlocks(&blocks, "", beforeLen)
|
||||
|
@ -1296,13 +1307,13 @@ func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, orderBy stri
|
|||
ret = []*Block{}
|
||||
}
|
||||
|
||||
matchedBlockCount, matchedRootCount = fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter)
|
||||
matchedBlockCount, matchedRootCount = fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter, ignoreFilter)
|
||||
return
|
||||
}
|
||||
|
||||
func fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter string) (matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter, ignoreFilter string) (matchedBlockCount, matchedRootCount int) {
|
||||
fieldFilter := fieldRegexp(exp)
|
||||
stmt := "SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter
|
||||
stmt := "SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter + ignoreFilter
|
||||
stmt += boxFilter + pathFilter
|
||||
result, _ := sql.QueryNoLimit(stmt)
|
||||
if 1 > len(result) {
|
||||
|
@ -1313,7 +1324,7 @@ func fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter string)
|
|||
return
|
||||
}
|
||||
|
||||
func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
query = stringQuery(query)
|
||||
table := "blocks_fts" // 大小写敏感
|
||||
if !Conf.Search.CaseSensitive {
|
||||
|
@ -1330,19 +1341,7 @@ func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy strin
|
|||
"fcontent, markdown, length, type, subtype, ial, sort, created, updated"
|
||||
stmt := "SELECT " + projections + " FROM " + table + " WHERE (`" + table + "` MATCH '" + columnFilter() + ":(" + query + ")'"
|
||||
stmt += ") AND type IN " + typeFilter
|
||||
stmt += boxFilter + pathFilter
|
||||
|
||||
if ignoreLines := getSearchIgnoreLines(); 0 < len(ignoreLines) {
|
||||
// Support ignore search results https://github.com/siyuan-note/siyuan/issues/10089
|
||||
buf := bytes.Buffer{}
|
||||
for _, line := range ignoreLines {
|
||||
buf.WriteString(" AND ")
|
||||
buf.WriteString(line)
|
||||
}
|
||||
stmt += buf.String()
|
||||
}
|
||||
|
||||
stmt += " " + orderBy
|
||||
stmt += boxFilter + pathFilter + ignoreFilter + " " + orderBy
|
||||
stmt += " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize)
|
||||
blocks := sql.SelectBlocksRawStmt(stmt, page, pageSize)
|
||||
ret = fromSQLBlocks(&blocks, "", beforeLen)
|
||||
|
@ -1350,11 +1349,11 @@ func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy strin
|
|||
ret = []*Block{}
|
||||
}
|
||||
|
||||
matchedBlockCount, matchedRootCount = fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter)
|
||||
matchedBlockCount, matchedRootCount = fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter, ignoreFilter)
|
||||
return
|
||||
}
|
||||
|
||||
func fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter string) (matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter, ignoreFilter string) (matchedBlockCount, matchedRootCount int) {
|
||||
if ast.IsNodeIDPattern(query) {
|
||||
ret, _ := sql.QueryNoLimit("SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `blocks` WHERE `id` = '" + query + "'")
|
||||
if 1 > len(ret) {
|
||||
|
@ -1372,18 +1371,7 @@ func fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter string) (
|
|||
|
||||
stmt := "SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `" + table + "` WHERE (`" + table + "` MATCH '" + columnFilter() + ":(" + query + ")'"
|
||||
stmt += ") AND type IN " + typeFilter
|
||||
stmt += boxFilter + pathFilter
|
||||
|
||||
if ignoreLines := getSearchIgnoreLines(); 0 < len(ignoreLines) {
|
||||
// Support ignore search results https://github.com/siyuan-note/siyuan/issues/10089
|
||||
buf := bytes.Buffer{}
|
||||
for _, line := range ignoreLines {
|
||||
buf.WriteString(" AND ")
|
||||
buf.WriteString(line)
|
||||
}
|
||||
stmt += buf.String()
|
||||
}
|
||||
|
||||
stmt += boxFilter + pathFilter + ignoreFilter
|
||||
result, _ := sql.QueryNoLimit(stmt)
|
||||
if 1 > len(result) {
|
||||
return
|
||||
|
@ -1393,23 +1381,12 @@ func fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter string) (
|
|||
return
|
||||
}
|
||||
|
||||
func fullTextSearchByFTSWithRoot(query, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
func fullTextSearchByFTSWithRoot(query, boxFilter, pathFilter, typeFilter, ignoreFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
|
||||
table := "blocks_fts" // 大小写敏感
|
||||
if !Conf.Search.CaseSensitive {
|
||||
table = "blocks_fts_case_insensitive"
|
||||
}
|
||||
|
||||
var ignoreFilter string
|
||||
if ignoreLines := getSearchIgnoreLines(); 0 < len(ignoreLines) {
|
||||
// Support ignore search results https://github.com/siyuan-note/siyuan/issues/10089
|
||||
buf := bytes.Buffer{}
|
||||
for _, line := range ignoreLines {
|
||||
buf.WriteString(" AND ")
|
||||
buf.WriteString(line)
|
||||
}
|
||||
ignoreFilter += buf.String()
|
||||
}
|
||||
|
||||
mQ := stringQuery(query)
|
||||
bMatchStmt := "SELECT id FROM " + table + " WHERE (" + table + " MATCH '" + columnFilter() + ":(" + mQ + ")')"
|
||||
bMatchStmt += " AND type IN " + typeFilter + boxFilter + pathFilter + ignoreFilter
|
||||
|
|
Loading…
Add table
Reference in a new issue