Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved search2 and search3 when there are multiple words searched on. #335

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions server/ctrlsubsonic/handlers_by_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,13 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
user := r.Context().Value(CtxUser).(*db.User)
query, err := params.Get("query")
var queries []string
if err != nil {
return spec.NewError(10, "please provide a `query` parameter")
}
query = fmt.Sprintf("%%%s%%", strings.Trim(query, `*"'`))
for _, s := range strings.Fields(query) {
queries = append(queries, fmt.Sprintf("%%%s%%", strings.Trim(s, `*"'`)))
}

results := &spec.SearchResultTwo{}

Expand All @@ -216,9 +219,11 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {
}

var artists []*db.Album
q := c.DB.
Where(`parent_id IN ? AND (right_path LIKE ? OR right_path_u_dec LIKE ?)`, rootQ.SubQuery(), query, query).
Preload("AlbumStar", "user_id=?", user.ID).
q := c.DB.Where(`parent_id IN ?`, rootQ.SubQuery())
for _, s := range queries {
q = q.Where(`right_path LIKE ? OR right_path_u_dec LIKE ?`, s, s)
}
q = q.Preload("AlbumStar", "user_id=?", user.ID).
Preload("AlbumRating", "user_id=?", user.ID).
Offset(params.GetOrInt("artistOffset", 0)).
Limit(params.GetOrInt("artistCount", 20))
Expand All @@ -231,9 +236,11 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {

// search "albums"
var albums []*db.Album
q = c.DB.
Where(`tag_artist_id IS NOT NULL AND (right_path LIKE ? OR right_path_u_dec LIKE ?)`, query, query).
Preload("AlbumStar", "user_id=?", user.ID).
q = c.DB.Where(`tag_artist_id IS NOT NULL`)
for _, s := range queries {
q = q.Where(`right_path LIKE ? OR right_path_u_dec LIKE ?`, s, s)
}
q = q.Preload("AlbumStar", "user_id=?", user.ID).
Preload("AlbumRating", "user_id=?", user.ID).
Offset(params.GetOrInt("albumOffset", 0)).
Limit(params.GetOrInt("albumCount", 20))
Expand All @@ -249,10 +256,11 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {

// search tracks
var tracks []*db.Track
q = c.DB.
Preload("Album").
Where("filename LIKE ? OR filename_u_dec LIKE ?", query, query).
Preload("TrackStar", "user_id=?", user.ID).
q = c.DB.Preload("Album")
for _, s := range queries {
q = q.Where(`filename LIKE ? OR filename LIKE ?`, s, s)
}
q = q.Preload("TrackStar", "user_id=?", user.ID).
Preload("TrackRating", "user_id=?", user.ID).
Offset(params.GetOrInt("songOffset", 0)).
Limit(params.GetOrInt("songCount", 20))
Expand Down
29 changes: 19 additions & 10 deletions server/ctrlsubsonic/handlers_by_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,25 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
user := r.Context().Value(CtxUser).(*db.User)
query, err := params.Get("query")
var queries []string
if err != nil {
return spec.NewError(10, "please provide a `query` parameter")
}
query = fmt.Sprintf("%%%s%%", strings.Trim(query, `*"'`))
for _, s := range strings.Fields(query) {
queries = append(queries, fmt.Sprintf("%%%s%%", strings.Trim(s, `*"'`)))
}

results := &spec.SearchResultThree{}

// search artists
var artists []*db.Artist
q := c.DB.
Select("*, count(albums.id) album_count").
Group("artists.id").
Where("name LIKE ? OR name_u_dec LIKE ?", query, query).
Joins("JOIN albums ON albums.tag_artist_id=artists.id").
Group("artists.id")
for _, s := range queries {
q = q.Where(`name LIKE ? OR name_u_dec LIKE ?`, s, s)
}
q = q.Joins("JOIN albums ON albums.tag_artist_id=artists.id").
Preload("ArtistStar", "user_id=?", user.ID).
Preload("ArtistRating", "user_id=?", user.ID).
Offset(params.GetOrInt("artistOffset", 0)).
Expand All @@ -241,9 +246,11 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
Preload("TagArtist").
Preload("Genres").
Preload("AlbumStar", "user_id=?", user.ID).
Preload("AlbumRating", "user_id=?", user.ID).
Where("tag_title LIKE ? OR tag_title_u_dec LIKE ?", query, query).
Offset(params.GetOrInt("albumOffset", 0)).
Preload("AlbumRating", "user_id=?", user.ID)
for _, s := range queries {
q = q.Where(`tag_title LIKE ? OR tag_title_u_dec LIKE ?`, s, s)
}
q = q.Offset(params.GetOrInt("albumOffset", 0)).
Limit(params.GetOrInt("albumCount", 20))
if m := getMusicFolder(c.MusicPaths, params); m != "" {
q = q.Where("root_dir=?", m)
Expand All @@ -262,9 +269,11 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
Preload("Album.TagArtist").
Preload("Genres").
Preload("TrackStar", "user_id=?", user.ID).
Preload("TrackRating", "user_id=?", user.ID).
Where("tag_title LIKE ? OR tag_title_u_dec LIKE ?", query, query).
Offset(params.GetOrInt("songOffset", 0)).
Preload("TrackRating", "user_id=?", user.ID)
for _, s := range queries {
q = q.Where(`tag_title LIKE ? OR tag_title_u_dec LIKE ?`, s, s)
}
q = q.Offset(params.GetOrInt("songOffset", 0)).
Limit(params.GetOrInt("songCount", 20))
if m := getMusicFolder(c.MusicPaths, params); m != "" {
q = q.
Expand Down