This commit is contained in:
Daniel 2024-05-26 17:41:35 +08:00
parent e8a501b6c7
commit c511795eb5
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017

View File

@ -853,7 +853,7 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
beforeLen := 36 beforeLen := 36
var blocks []*Block var blocks []*Block
orderByClause := buildOrderBy(method, orderBy) orderByClause := buildOrderBy(query, method, orderBy)
switch method { switch method {
case 1: // 查询语法 case 1: // 查询语法
filter := buildTypeFilter(types) filter := buildTypeFilter(types)
@ -992,7 +992,7 @@ func buildPathsFilter(paths []string) string {
return builder.String() return builder.String()
} }
func buildOrderBy(method, orderBy int) string { func buildOrderBy(query string, method, orderBy int) string {
switch orderBy { switch orderBy {
case 1: case 1:
return "ORDER BY created ASC" return "ORDER BY created ASC"
@ -1014,7 +1014,14 @@ func buildOrderBy(method, orderBy int) string {
} }
return "ORDER BY rank" // 默认是按相关度降序 return "ORDER BY rank" // 默认是按相关度降序
default: default:
return "ORDER BY sort ASC, updated DESC" // Improve search default sort https://github.com/siyuan-note/siyuan/issues/8624 clause := "ORDER BY CASE " +
"WHEN name = '${keyword}' THEN 10 " +
"WHEN alias = '${keyword}' THEN 20 " +
"WHEN name LIKE '%${keyword}%' THEN 50 " +
"WHEN alias LIKE '%${keyword}%' THEN 60 " +
"ELSE 65535 END ASC, sort ASC, updated DESC"
clause = strings.ReplaceAll(clause, "${keyword}", strings.ReplaceAll(query, "'", "''"))
return clause
} }
} }
@ -1146,21 +1153,21 @@ func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret []
stmt += notLike.String() stmt += notLike.String()
} }
orderBy := ` order by case orderBy := ` ORDER BY CASE
when name = '${keyword}' then 10 WHEN name = '${keyword}' THEN 10
when alias = '${keyword}' then 20 WHEN alias = '${keyword}' THEN 20
when memo = '${keyword}' then 30 WHEN memo = '${keyword}' THEN 30
when content = '${keyword}' and type = 'd' then 40 WHEN content = '${keyword}' and type = 'd' THEN 40
when content LIKE '%${keyword}%' and type = 'd' then 41 WHEN content LIKE '%${keyword}%' and type = 'd' THEN 41
when name LIKE '%${keyword}%' then 50 WHEN name LIKE '%${keyword}%' THEN 50
when alias LIKE '%${keyword}%' then 60 WHEN alias LIKE '%${keyword}%' THEN 60
when content = '${keyword}' and type = 'h' then 70 WHEN content = '${keyword}' and type = 'h' THEN 70
when content LIKE '%${keyword}%' and type = 'h' then 71 WHEN content LIKE '%${keyword}%' and type = 'h' THEN 71
when fcontent = '${keyword}' and type = 'i' then 80 WHEN fcontent = '${keyword}' and type = 'i' THEN 80
when fcontent LIKE '%${keyword}%' and type = 'i' then 81 WHEN fcontent LIKE '%${keyword}%' and type = 'i' THEN 81
when memo LIKE '%${keyword}%' then 90 WHEN memo LIKE '%${keyword}%' THEN 90
when content LIKE '%${keyword}%' and type != 'i' and type != 'l' then 100 WHEN content LIKE '%${keyword}%' and type != 'i' and type != 'l' THEN 100
else 65535 end ASC, sort ASC, length ASC` ELSE 65535 END ASC, sort ASC, length ASC`
orderBy = strings.ReplaceAll(orderBy, "${keyword}", strings.ReplaceAll(keyword, "'", "''")) orderBy = strings.ReplaceAll(orderBy, "${keyword}", strings.ReplaceAll(keyword, "'", "''"))
stmt += orderBy + " LIMIT " + strconv.Itoa(Conf.Search.Limit) stmt += orderBy + " LIMIT " + strconv.Itoa(Conf.Search.Limit)
blocks := sql.SelectBlocksRawStmtNoParse(stmt, Conf.Search.Limit) blocks := sql.SelectBlocksRawStmtNoParse(stmt, Conf.Search.Limit)