feat: search in WebUI now is more flexible, searching in all relevant fields in the current view

This commit is contained in:
Deluan
2020-03-19 22:26:18 -04:00
parent 32fbf2e9eb
commit 8401d85f78
6 changed files with 25 additions and 6 deletions
+1
View File
@@ -24,6 +24,7 @@ func NewAlbumRepository(ctx context.Context, o orm.Ormer) model.AlbumRepository
"artist": "compilation asc, album_artist asc, name asc",
}
r.filterMappings = map[string]filterFunc{
"name": fullTextFilter,
"compilation": booleanFilter,
}
+3
View File
@@ -25,6 +25,9 @@ func NewArtistRepository(ctx context.Context, o orm.Ormer) model.ArtistRepositor
r.ormer = o
r.indexGroups = utils.ParseIndexGroups(conf.Server.IndexGroups)
r.tableName = "artist"
r.filterMappings = map[string]filterFunc{
"name": fullTextFilter,
}
return r
}
+3
View File
@@ -25,6 +25,9 @@ func NewMediaFileRepository(ctx context.Context, o orm.Ormer) *mediaFileReposito
"artist": "artist asc, album asc, disc_number asc, track_number asc",
"album": "album asc, disc_number asc, track_number asc",
}
r.filterMappings = map[string]filterFunc{
"title": fullTextFilter,
}
return r
}
+15
View File
@@ -13,6 +13,7 @@ import (
"github.com/deluan/navidrome/model"
"github.com/deluan/rest"
"github.com/google/uuid"
"github.com/kennygrant/sanitize"
)
type filterFunc = func(field string, value interface{}) Sqlizer
@@ -249,3 +250,17 @@ func booleanFilter(field string, value interface{}) Sqlizer {
v := strings.ToLower(value.(string))
return Eq{field: strings.ToLower(v) == "true"}
}
func fullTextFilter(field string, value interface{}) Sqlizer {
q := value.(string)
q = strings.TrimSpace(sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))))
parts := strings.Split(q, " ")
filters := And{}
for _, part := range parts {
filters = append(filters, Or{
Like{"full_text": part + "%"},
Like{"full_text": "%" + part + "%"},
})
}
return filters
}