mirror of
https://github.com/navidrome/navidrome.git
synced 2026-02-25 19:36:11 -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>
107 lines
3.6 KiB
Go
107 lines
3.6 KiB
Go
package persistence
|
|
|
|
import (
|
|
"strings"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/str"
|
|
)
|
|
|
|
// likeSearch implements searchStrategy using LIKE-based SQL filters.
|
|
// Used for legacy full_text searches, CJK fallback, and punctuation-only fallback.
|
|
type likeSearch struct {
|
|
filter Sqlizer
|
|
}
|
|
|
|
func (s *likeSearch) ToSql() (string, []interface{}, error) {
|
|
return s.filter.ToSql()
|
|
}
|
|
|
|
func (s *likeSearch) execute(r sqlRepository, sq SelectBuilder, dest any, cfg searchConfig, options model.QueryOptions) error {
|
|
sq = sq.Where(s.filter)
|
|
sq = sq.OrderBy(cfg.OrderBy...)
|
|
return r.queryAll(sq, dest, options)
|
|
}
|
|
|
|
// newLegacySearch creates a LIKE search against the full_text column.
|
|
// Returns nil when the query produces no searchable tokens.
|
|
func newLegacySearch(tableName, query string) searchStrategy {
|
|
filter := legacySearchExpr(tableName, query)
|
|
if filter == nil {
|
|
return nil
|
|
}
|
|
return &likeSearch{filter: filter}
|
|
}
|
|
|
|
// newLikeSearch creates a LIKE search against core entity columns (CJK, punctuation fallback).
|
|
// No minimum length is enforced, since single CJK characters are meaningful words.
|
|
// Returns nil when the query produces no searchable tokens.
|
|
func newLikeSearch(tableName, query string) searchStrategy {
|
|
filter := likeSearchExpr(tableName, query)
|
|
if filter == nil {
|
|
return nil
|
|
}
|
|
return &likeSearch{filter: filter}
|
|
}
|
|
|
|
// legacySearchExpr generates LIKE-based search filters against the full_text column.
|
|
// This is the original search implementation, used when Search.Backend="legacy".
|
|
func legacySearchExpr(tableName string, s string) Sqlizer {
|
|
q := str.SanitizeStrings(s)
|
|
if q == "" {
|
|
log.Trace("Search using legacy backend, query is empty", "table", tableName)
|
|
return nil
|
|
}
|
|
var sep string
|
|
if !conf.Server.Search.FullString {
|
|
sep = " "
|
|
}
|
|
parts := strings.Split(q, " ")
|
|
filters := And{}
|
|
for _, part := range parts {
|
|
filters = append(filters, Like{tableName + ".full_text": "%" + sep + part + "%"})
|
|
}
|
|
log.Trace("Search using legacy backend", "query", filters, "table", tableName)
|
|
return filters
|
|
}
|
|
|
|
// likeSearchColumns defines the core columns to search with LIKE queries.
|
|
// These are the primary user-visible fields for each entity type.
|
|
// Used as a fallback when FTS5 cannot handle the query (e.g., CJK text, punctuation-only input).
|
|
var likeSearchColumns = map[string][]string{
|
|
"media_file": {"title", "album", "artist", "album_artist"},
|
|
"album": {"name", "album_artist"},
|
|
"artist": {"name"},
|
|
}
|
|
|
|
// likeSearchExpr generates LIKE-based search filters against core columns.
|
|
// Each word in the query must match at least one column (AND between words),
|
|
// and each word can match any column (OR within a word).
|
|
// Used as a fallback when FTS5 cannot handle the query (e.g., CJK text, punctuation-only input).
|
|
func likeSearchExpr(tableName string, s string) Sqlizer {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
log.Trace("Search using LIKE backend, query is empty", "table", tableName)
|
|
return nil
|
|
}
|
|
columns, ok := likeSearchColumns[tableName]
|
|
if !ok {
|
|
log.Trace("Search using LIKE backend, couldn't find columns for this table", "table", tableName)
|
|
return nil
|
|
}
|
|
words := strings.Fields(s)
|
|
wordFilters := And{}
|
|
for _, word := range words {
|
|
colFilters := Or{}
|
|
for _, col := range columns {
|
|
colFilters = append(colFilters, Like{tableName + "." + col: "%" + word + "%"})
|
|
}
|
|
wordFilters = append(wordFilters, colFilters)
|
|
}
|
|
log.Trace("Search using LIKE backend", "query", wordFilters, "table", tableName)
|
|
return wordFilters
|
|
}
|