mirror of
https://github.com/navidrome/navidrome.git
synced 2026-02-23 18:38:34 -05:00
* fix(subsonic): optimize search3 for high-cardinality FTS queries Use a two-phase query strategy for FTS5 searches to avoid the performance penalty of expensive LEFT JOINs (annotation, bookmark, library) on high-cardinality results like "the". Phase 1 runs a lightweight query (main table + FTS index only) to get sorted, paginated rowids. Phase 2 hydrates only those few rowids with the full JOINs, making them nearly free. For queries with complex ORDER BY expressions that reference joined tables (e.g. artist search sorted by play count), the optimization is skipped and the original single-query approach is used. * fix(search): update order by clauses to include 'rank' for FTS queries Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): reintroduce 'rank' in Phase 2 ORDER BY for FTS queries Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): remove 'rank' from ORDER BY in non-FTS queries and adjust two-phase query handling Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): update FTS ranking to use bm25 weights and simplify ORDER BY qualification Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): refine FTS query handling and improve comments for clarity Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): refactor full-text search handling to streamline query strategy selection and improve LIKE fallback logic. Increase e2e coverage for search3 Signed-off-by: Deluan <deluan@navidrome.org> * refactor: enhance FTS column definitions and relevance weights Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): refactor Search method signatures to remove offset and size parameters, streamline query handling Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): allow single-character queries in search strategies and update related tests Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): make FTS Phase 1 treat Max=0 as no limit, reorganize tests FTS Phase 1 unconditionally called Limit(uint64(options.Max)), which produced LIMIT 0 when Max was zero. This diverged from applyOptions where Max=0 means no limit. Now Phase 1 mirrors applyOptions: only add LIMIT/OFFSET when the value is positive. Also moved legacy backend integration tests from sql_search_fts_test.go to sql_search_like_test.go and added regression tests for the Max=0 behavior on both backends. * refactor: simplify callSearch function by removing variadic options and directly using QueryOptions Signed-off-by: Deluan <deluan@navidrome.org> * fix(search): implement ftsQueryDegraded function to detect significant content loss in FTS queries Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
180 lines
4.4 KiB
Go
180 lines
4.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/deluan/rest"
|
|
"github.com/fatih/structs"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
)
|
|
|
|
type filterFunc = func(field string, value any) Sqlizer
|
|
|
|
func (r *sqlRepository) parseRestFilters(ctx context.Context, options rest.QueryOptions) Sqlizer {
|
|
if len(options.Filters) == 0 {
|
|
return nil
|
|
}
|
|
filters := And{}
|
|
for f, v := range options.Filters {
|
|
// Ignore filters with empty values
|
|
if v == "" {
|
|
continue
|
|
}
|
|
// Look for a custom filter function
|
|
f = strings.ToLower(f)
|
|
if ff, ok := r.filterMappings[f]; ok {
|
|
if filter := ff(f, v); filter != nil {
|
|
filters = append(filters, filter)
|
|
}
|
|
continue
|
|
}
|
|
// Ignore invalid filters (not based on a field or filter function)
|
|
if r.isFieldWhiteListed != nil && !r.isFieldWhiteListed(f) {
|
|
log.Warn(ctx, "Ignoring filter not whitelisted", "filter", f, "table", r.tableName)
|
|
continue
|
|
}
|
|
// For fields ending in "id", use an exact match
|
|
if strings.HasSuffix(f, "id") {
|
|
filters = append(filters, eqFilter(f, v))
|
|
continue
|
|
}
|
|
// Default to a "starts with" filter
|
|
filters = append(filters, startsWithFilter(f, v))
|
|
}
|
|
return filters
|
|
}
|
|
|
|
func (r *sqlRepository) parseRestOptions(ctx context.Context, options ...rest.QueryOptions) model.QueryOptions {
|
|
qo := model.QueryOptions{}
|
|
if len(options) > 0 {
|
|
qo.Sort, qo.Order = r.sanitizeSort(options[0].Sort, options[0].Order)
|
|
qo.Max = options[0].Max
|
|
qo.Offset = options[0].Offset
|
|
if seed, ok := options[0].Filters["seed"].(string); ok {
|
|
qo.Seed = seed
|
|
delete(options[0].Filters, "seed")
|
|
}
|
|
qo.Filters = r.parseRestFilters(ctx, options[0])
|
|
}
|
|
return qo
|
|
}
|
|
|
|
func (r sqlRepository) sanitizeSort(sort, order string) (string, string) {
|
|
if sort != "" {
|
|
sort = toSnakeCase(sort)
|
|
if mapped, ok := r.sortMappings[sort]; ok {
|
|
sort = mapped
|
|
} else {
|
|
if !r.isFieldWhiteListed(sort) {
|
|
log.Warn(r.ctx, "Ignoring sort not whitelisted", "sort", sort, "table", r.tableName)
|
|
sort = ""
|
|
}
|
|
}
|
|
}
|
|
if order != "" {
|
|
order = strings.ToLower(order)
|
|
if order != "desc" {
|
|
order = "asc"
|
|
}
|
|
}
|
|
return sort, order
|
|
}
|
|
|
|
func eqFilter(field string, value any) Sqlizer {
|
|
return Eq{field: value}
|
|
}
|
|
|
|
func startsWithFilter(field string, value any) Sqlizer {
|
|
return Like{field: fmt.Sprintf("%s%%", value)}
|
|
}
|
|
|
|
func containsFilter(field string) func(string, any) Sqlizer {
|
|
return func(_ string, value any) Sqlizer {
|
|
return Like{field: fmt.Sprintf("%%%s%%", value)}
|
|
}
|
|
}
|
|
|
|
func booleanFilter(field string, value any) Sqlizer {
|
|
v := strings.ToLower(value.(string))
|
|
return Eq{field: v == "true"}
|
|
}
|
|
|
|
func fullTextFilter(tableName string, mbidFields ...string) func(string, any) Sqlizer {
|
|
return func(field string, value any) Sqlizer {
|
|
v := strings.ToLower(value.(string))
|
|
return cmp.Or[Sqlizer](
|
|
mbidExpr(tableName, v, mbidFields...),
|
|
getSearchStrategy(tableName, v),
|
|
)
|
|
}
|
|
}
|
|
|
|
func substringFilter(field string, value any) Sqlizer {
|
|
parts := strings.Fields(value.(string))
|
|
filters := And{}
|
|
for _, part := range parts {
|
|
filters = append(filters, Like{field: "%" + part + "%"})
|
|
}
|
|
return filters
|
|
}
|
|
|
|
func idFilter(tableName string) func(string, any) Sqlizer {
|
|
return func(field string, value any) Sqlizer { return Eq{tableName + ".id": value} }
|
|
}
|
|
|
|
func invalidFilter(ctx context.Context) func(string, any) Sqlizer {
|
|
return func(field string, value any) Sqlizer {
|
|
log.Warn(ctx, "Invalid filter", "fieldName", field, "value", value)
|
|
return Eq{"1": "0"}
|
|
}
|
|
}
|
|
|
|
var (
|
|
whiteList = map[string]map[string]struct{}{}
|
|
mutex sync.RWMutex
|
|
)
|
|
|
|
func registerModelWhiteList(instance any) fieldWhiteListedFunc {
|
|
name := reflect.TypeOf(instance).String()
|
|
registerFieldWhiteList(name, instance)
|
|
return getFieldWhiteListedFunc(name)
|
|
}
|
|
|
|
func registerFieldWhiteList(name string, instance any) {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
if whiteList[name] != nil {
|
|
return
|
|
}
|
|
m := structs.Map(instance)
|
|
whiteList[name] = map[string]struct{}{}
|
|
for k := range m {
|
|
whiteList[name][toSnakeCase(k)] = struct{}{}
|
|
}
|
|
ma := structs.Map(model.Annotations{})
|
|
for k := range ma {
|
|
whiteList[name][toSnakeCase(k)] = struct{}{}
|
|
}
|
|
}
|
|
|
|
type fieldWhiteListedFunc func(field string) bool
|
|
|
|
func getFieldWhiteListedFunc(tableName string) fieldWhiteListedFunc {
|
|
return func(field string) bool {
|
|
mutex.RLock()
|
|
defer mutex.RUnlock()
|
|
if _, ok := whiteList[tableName]; !ok {
|
|
return false
|
|
}
|
|
_, ok := whiteList[tableName][field]
|
|
return ok
|
|
}
|
|
}
|