fix(search): apply the artist library scope to every search path - #6340 (#6415)

* fix(persistence): apply the artist library scope to every search path - #6340

Signed-off-by: yuwk <1729065730@qq.com>

* test(persistence): expect a scoped artist search to find the artist

The library-scope specs only showed that searching another library
returns nothing. Also search with library_id = [lib2.ID] and expect
the artist that belongs to that library.

Signed-off-by: Vincent <52391318+ump45nose@users.noreply.github.com>
Co-authored-by: ump45nose <ump45nose@users.noreply.github.com>

* refactor(search): streamline artist search configuration and improve library filtering logic

---------

Signed-off-by: yuwk <1729065730@qq.com>
Signed-off-by: Vincent <52391318+ump45nose@users.noreply.github.com>
Co-authored-by: Deluan Quintão <deluan@navidrome.org>
Co-authored-by: ump45nose <ump45nose@users.noreply.github.com>
This commit is contained in:
ump45nose 2026-11-10 00:35:05 +08:00 committed by GitHub
parent 93e01f390e
commit e40d54fd40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 3 deletions

View File

@ -613,7 +613,7 @@ func (r *artistRepository) RefreshStats(ctx context.Context, allArtists bool) (i
return totalRowsAffected, nil
}
// searchCfg builds the per-search config. scope is the set of library IDs the rowid Phase 1 must
// searchCfg builds the per-search config. scope is the set of library IDs the search must
// restrict artists to, or nil to skip the filter (fast-path). See [artistRepository.searchScope].
func (r *artistRepository) searchCfg(scope []int) searchConfig {
return searchConfig{
@ -655,13 +655,16 @@ func (r *artistRepository) Search(ctx context.Context, q string, options ...mode
opts = options[0]
}
// Artists have no library_id column, so the library_id filter callers pass (same as albums/songs)
// can't be applied directly: consume it and realize it as a join-free Phase-1 scope (searchCfg).
// can't be applied directly: consume it and realize it as a join-free scope (searchCfg).
scope := r.searchScope(ctx, opts.Filters)
if isLibraryIDFilter(opts.Filters) {
opts.Filters = nil
}
cfg := r.searchCfg(scope)
// Also scope the full SELECT, like albums/songs do: the LIKE and MBID paths have no Phase 1.
sq := cfg.LibraryFilter(r.selectArtist(ctx, opts))
var res dbArtists
err := r.doSearch(ctx, r.selectArtist(ctx, opts), q, &res, r.searchCfg(scope), opts)
err := r.doSearch(ctx, sq, q, &res, cfg, opts)
if err != nil {
return nil, fmt.Errorf("searching artist %q: %w", q, err)
}

View File

@ -830,6 +830,36 @@ var _ = Describe("ArtistRepository", func() {
_, _ = raw.executeSQL(ctx, squirrel.Delete(raw.tableName).Where(squirrel.Eq{"id": inaccessibleArtist.ID}))
}
})
DescribeTable("narrows artist search to the requested library",
func(backend string, artist model.Artist, query string) {
DeferCleanup(configtest.SetupConfig())
if backend != "" {
conf.Server.Search.Backend = backend
}
Expect(createArtistWithLibrary(ctx, repo, &artist, lib2.ID)).To(Succeed())
DeferCleanup(func() {
raw := repo.(*artistRepository)
_, _ = raw.executeSQL(ctx, squirrel.Delete(raw.tableName).Where(squirrel.Eq{"id": artist.ID}))
})
search := func(filters squirrel.Sqlizer) model.Artists {
results, err := repo.Search(ctx, query, model.QueryOptions{Max: 100, Filters: filters})
Expect(err).ToNot(HaveOccurred())
return results
}
Expect(search(nil)).To(HaveLen(1))
Expect(search(squirrel.Eq{"library_id": []int{1}})).To(BeEmpty())
Expect(search(squirrel.Eq{"library_id": []int{lib2.ID}})).To(HaveLen(1))
},
Entry("legacy (LIKE) backend", "legacy",
model.Artist{ID: "legacy-search-lib2-artist", Name: "Folder Scope Zeta Artist"}, "Folder Scope"),
Entry("CJK LIKE fallback", "",
model.Artist{ID: "cjk-search-lib2-artist", Name: "文件夹范围测试艺术家"}, "文件夹"),
Entry("MBID match", "",
createTestArtistWithMBID("mbid-search-lib2-artist", "Folder Scope MBID Artist", "3f2b1e58-8a4c-4a1b-9c3d-7e2f5a1b9c04"),
"3f2b1e58-8a4c-4a1b-9c3d-7e2f5a1b9c04"),
)
})
Context("Empty Query (sync pagination)", func() {