🎨 Return document blocks when search hits different block content https://github.com/siyuan-note/siyuan/issues/10584

This commit is contained in:
Daniel 2024-10-24 00:27:26 +08:00
parent 3ba6dfed83
commit da341269cd
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017

View File

@ -914,27 +914,39 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
query = trimQuery 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 beforeLen := 36
var blocks []*Block var blocks []*Block
orderByClause := buildOrderBy(query, method, orderBy) orderByClause := buildOrderBy(query, method, orderBy)
switch method { switch method {
case 1: // 查询语法 case 1: // 查询语法
filter := buildTypeFilter(types) typeFilter := buildTypeFilter(types)
boxFilter := buildBoxesFilter(boxes) boxFilter := buildBoxesFilter(boxes)
pathFilter := buildPathsFilter(paths) 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 case 2: // SQL
blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen, page, pageSize) blocks, matchedBlockCount, matchedRootCount = searchBySQL(query, beforeLen, page, pageSize)
case 3: // 正则表达式 case 3: // 正则表达式
typeFilter := buildTypeFilter(types) typeFilter := buildTypeFilter(types)
boxFilter := buildBoxesFilter(boxes) boxFilter := buildBoxesFilter(boxes)
pathFilter := buildPathsFilter(paths) 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: // 关键字 default: // 关键字
filter := buildTypeFilter(types) typeFilter := buildTypeFilter(types)
boxFilter := buildBoxesFilter(boxes) boxFilter := buildBoxesFilter(boxes)
pathFilter := buildPathsFilter(paths) 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 pageCount = (matchedBlockCount + pageSize - 1) / pageSize
@ -1264,31 +1276,30 @@ func extractID(content string) (ret string) {
return 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) query = filterQueryInvisibleChars(query)
if ast.IsNodeIDPattern(query) { if ast.IsNodeIDPattern(query) {
ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize) ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize)
return 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) query = filterQueryInvisibleChars(query)
if ast.IsNodeIDPattern(query) { if ast.IsNodeIDPattern(query) {
ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize) ret, matchedBlockCount, matchedRootCount = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+query+"'", beforeLen, page, pageSize)
return 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) exp = filterQueryInvisibleChars(exp)
fieldFilter := fieldRegexp(exp) fieldFilter := fieldRegexp(exp)
stmt := "SELECT * FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter stmt := "SELECT * FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter
stmt += boxFilter + pathFilter stmt += boxFilter + pathFilter + ignoreFilter + " " + orderBy
stmt += " " + orderBy
regex := regexp.MustCompile(exp) regex := regexp.MustCompile(exp)
blocks := sql.SelectBlocksRegex(stmt, regex, Conf.Search.Name, Conf.Search.Alias, Conf.Search.Memo, Conf.Search.IAL, page, pageSize) blocks := sql.SelectBlocksRegex(stmt, regex, Conf.Search.Name, Conf.Search.Alias, Conf.Search.Memo, Conf.Search.IAL, page, pageSize)
ret = fromSQLBlocks(&blocks, "", beforeLen) ret = fromSQLBlocks(&blocks, "", beforeLen)
@ -1296,13 +1307,13 @@ func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, orderBy stri
ret = []*Block{} ret = []*Block{}
} }
matchedBlockCount, matchedRootCount = fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter) matchedBlockCount, matchedRootCount = fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter, ignoreFilter)
return 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) 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 stmt += boxFilter + pathFilter
result, _ := sql.QueryNoLimit(stmt) result, _ := sql.QueryNoLimit(stmt)
if 1 > len(result) { if 1 > len(result) {
@ -1313,7 +1324,7 @@ func fullTextSearchCountByRegexp(exp, boxFilter, pathFilter, typeFilter string)
return 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) query = stringQuery(query)
table := "blocks_fts" // 大小写敏感 table := "blocks_fts" // 大小写敏感
if !Conf.Search.CaseSensitive { 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" "fcontent, markdown, length, type, subtype, ial, sort, created, updated"
stmt := "SELECT " + projections + " FROM " + table + " WHERE (`" + table + "` MATCH '" + columnFilter() + ":(" + query + ")'" stmt := "SELECT " + projections + " FROM " + table + " WHERE (`" + table + "` MATCH '" + columnFilter() + ":(" + query + ")'"
stmt += ") AND type IN " + typeFilter stmt += ") AND type IN " + typeFilter
stmt += boxFilter + pathFilter stmt += boxFilter + pathFilter + ignoreFilter + " " + orderBy
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 += " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize) stmt += " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize)
blocks := sql.SelectBlocksRawStmt(stmt, page, pageSize) blocks := sql.SelectBlocksRawStmt(stmt, page, pageSize)
ret = fromSQLBlocks(&blocks, "", beforeLen) ret = fromSQLBlocks(&blocks, "", beforeLen)
@ -1350,11 +1349,11 @@ func fullTextSearchByFTS(query, boxFilter, pathFilter, typeFilter, orderBy strin
ret = []*Block{} ret = []*Block{}
} }
matchedBlockCount, matchedRootCount = fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter) matchedBlockCount, matchedRootCount = fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter, ignoreFilter)
return 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) { if ast.IsNodeIDPattern(query) {
ret, _ := sql.QueryNoLimit("SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `blocks` WHERE `id` = '" + query + "'") ret, _ := sql.QueryNoLimit("SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `blocks` WHERE `id` = '" + query + "'")
if 1 > len(ret) { 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 := "SELECT COUNT(id) AS `matches`, COUNT(DISTINCT(root_id)) AS `docs` FROM `" + table + "` WHERE (`" + table + "` MATCH '" + columnFilter() + ":(" + query + ")'"
stmt += ") AND type IN " + typeFilter stmt += ") AND type IN " + typeFilter
stmt += boxFilter + pathFilter stmt += boxFilter + pathFilter + ignoreFilter
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()
}
result, _ := sql.QueryNoLimit(stmt) result, _ := sql.QueryNoLimit(stmt)
if 1 > len(result) { if 1 > len(result) {
return return
@ -1393,23 +1381,12 @@ func fullTextSearchCountByFTS(query, boxFilter, pathFilter, typeFilter string) (
return 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" // 大小写敏感 table := "blocks_fts" // 大小写敏感
if !Conf.Search.CaseSensitive { if !Conf.Search.CaseSensitive {
table = "blocks_fts_case_insensitive" 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) mQ := stringQuery(query)
bMatchStmt := "SELECT id FROM " + table + " WHERE (" + table + " MATCH '" + columnFilter() + ":(" + mQ + ")')" bMatchStmt := "SELECT id FROM " + table + " WHERE (" + table + " MATCH '" + columnFilter() + ":(" + mQ + ")')"
bMatchStmt += " AND type IN " + typeFilter + boxFilter + pathFilter + ignoreFilter bMatchStmt += " AND type IN " + typeFilter + boxFilter + pathFilter + ignoreFilter