mirror of
https://github.com/navidrome/navidrome.git
synced 2026-02-24 02:48:29 -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>
135 lines
4.4 KiB
Go
135 lines
4.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("newLegacySearch", func() {
|
|
It("returns non-nil for single-character query", func() {
|
|
strategy := newLegacySearch("media_file", "a")
|
|
Expect(strategy).ToNot(BeNil(), "single-char queries must not be rejected; min-length is enforced in doSearch, not here")
|
|
sql, _, err := strategy.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sql).To(ContainSubstring("LIKE"))
|
|
})
|
|
})
|
|
|
|
var _ = Describe("legacySearchExpr", func() {
|
|
It("returns nil for empty query", func() {
|
|
Expect(legacySearchExpr("media_file", "")).To(BeNil())
|
|
})
|
|
|
|
It("generates LIKE filter for single word", func() {
|
|
expr := legacySearchExpr("media_file", "beatles")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sql).To(ContainSubstring("media_file.full_text LIKE"))
|
|
Expect(args).To(ContainElement("% beatles%"))
|
|
})
|
|
|
|
It("generates AND of LIKE filters for multiple words", func() {
|
|
expr := legacySearchExpr("media_file", "abbey road")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sql).To(ContainSubstring("AND"))
|
|
Expect(args).To(HaveLen(2))
|
|
})
|
|
})
|
|
|
|
var _ = Describe("likeSearchExpr", func() {
|
|
It("returns nil for empty query", func() {
|
|
Expect(likeSearchExpr("media_file", "")).To(BeNil())
|
|
})
|
|
|
|
It("returns nil for whitespace-only query", func() {
|
|
Expect(likeSearchExpr("media_file", " ")).To(BeNil())
|
|
})
|
|
|
|
It("generates LIKE filters against core columns for single CJK word", func() {
|
|
expr := likeSearchExpr("media_file", "周杰伦")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// Should have OR between columns for the single word
|
|
Expect(sql).To(ContainSubstring("OR"))
|
|
Expect(sql).To(ContainSubstring("media_file.title LIKE"))
|
|
Expect(sql).To(ContainSubstring("media_file.album LIKE"))
|
|
Expect(sql).To(ContainSubstring("media_file.artist LIKE"))
|
|
Expect(sql).To(ContainSubstring("media_file.album_artist LIKE"))
|
|
Expect(args).To(HaveLen(4))
|
|
for _, arg := range args {
|
|
Expect(arg).To(Equal("%周杰伦%"))
|
|
}
|
|
})
|
|
|
|
It("generates AND of OR groups for multi-word query", func() {
|
|
expr := likeSearchExpr("media_file", "周杰伦 greatest")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// Two groups AND'd together, each with 4 columns OR'd
|
|
Expect(sql).To(ContainSubstring("AND"))
|
|
Expect(args).To(HaveLen(8))
|
|
})
|
|
|
|
It("uses correct columns for album table", func() {
|
|
expr := likeSearchExpr("album", "周杰伦")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sql).To(ContainSubstring("album.name LIKE"))
|
|
Expect(sql).To(ContainSubstring("album.album_artist LIKE"))
|
|
Expect(args).To(HaveLen(2))
|
|
})
|
|
|
|
It("uses correct columns for artist table", func() {
|
|
expr := likeSearchExpr("artist", "周杰伦")
|
|
sql, args, err := expr.ToSql()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sql).To(ContainSubstring("artist.name LIKE"))
|
|
Expect(args).To(HaveLen(1))
|
|
})
|
|
|
|
It("returns nil for unknown table", func() {
|
|
Expect(likeSearchExpr("unknown_table", "周杰伦")).To(BeNil())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("Legacy Integration Search", func() {
|
|
var mr model.MediaFileRepository
|
|
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
conf.Server.Search.Backend = "legacy"
|
|
|
|
ctx := log.NewContext(context.TODO())
|
|
ctx = request.WithUser(ctx, adminUser)
|
|
conn := GetDBXBuilder()
|
|
mr = NewMediaFileRepository(ctx, conn)
|
|
})
|
|
|
|
It("returns results using legacy LIKE-based search", func() {
|
|
results, err := mr.Search("Radioactivity", model.QueryOptions{Max: 10})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(HaveLen(1))
|
|
Expect(results[0].Title).To(Equal("Radioactivity"))
|
|
})
|
|
|
|
It("returns empty results for single-char query (doSearch min-length guard)", func() {
|
|
results, err := mr.Search("a", model.QueryOptions{Max: 10})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).To(BeEmpty(), "doSearch should reject single-char queries")
|
|
})
|
|
|
|
It("returns results with Max=0 (regression: must not produce LIMIT 0)", func() {
|
|
results, err := mr.Search("Beatles", model.QueryOptions{Max: 0})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(results).ToNot(BeEmpty(), "Max=0 should mean no limit, not LIMIT 0")
|
|
})
|
|
})
|