diff --git a/conf/configuration.go b/conf/configuration.go index 0e3a9b4a..2393db0d 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -28,6 +28,7 @@ type configOptions struct { ImageCacheSize string AutoImportPlaylists bool + SearchFullString bool IgnoredArticles string IndexGroups string ProbeCommand string @@ -87,6 +88,7 @@ func init() { viper.SetDefault("autoimportplaylists", true) // Config options only valid for file/env configuration + viper.SetDefault("searchfullstring", false) viper.SetDefault("ignoredarticles", "The El La Los Las Le Les Os As O A") viper.SetDefault("indexgroups", "A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)") viper.SetDefault("probecommand", "ffmpeg %s -f ffmetadata") diff --git a/persistence/sql_restful.go b/persistence/sql_restful.go index c5208a96..13a7433c 100644 --- a/persistence/sql_restful.go +++ b/persistence/sql_restful.go @@ -62,11 +62,5 @@ func booleanFilter(field string, value interface{}) Sqlizer { } func fullTextFilter(field string, value interface{}) Sqlizer { - q := sanitizeStrings(value.(string)) - parts := strings.Split(q, " ") - filters := And{} - for _, part := range parts { - filters = append(filters, Like{"full_text": "% " + part + "%"}) - } - return filters + return fullTextExpr(value.(string)) } diff --git a/persistence/sql_search.go b/persistence/sql_search.go index 55c5b858..21eaa39b 100644 --- a/persistence/sql_search.go +++ b/persistence/sql_search.go @@ -6,6 +6,7 @@ import ( "strings" . "github.com/Masterminds/squirrel" + "github.com/deluan/navidrome/conf" "github.com/kennygrant/sanitize" ) @@ -37,20 +38,32 @@ func sanitizeStrings(text ...string) string { } func (r sqlRepository) doSearch(q string, offset, size int, results interface{}, orderBys ...string) error { + q = strings.TrimSpace(q) q = strings.TrimSuffix(q, "*") - q = sanitizeStrings(q) if len(q) < 2 { return nil } + sq := r.newSelectWithAnnotation(r.tableName + ".id").Columns("*") sq = sq.Limit(uint64(size)).Offset(uint64(offset)) if len(orderBys) > 0 { sq = sq.OrderBy(orderBys...) } - parts := strings.Split(q, " ") - for _, part := range parts { - sq = sq.Where(Like{"full_text": "% " + part + "%"}) - } + sq = sq.Where(fullTextExpr(q)) err := r.queryAll(sq, results) return err } + +func fullTextExpr(value string) Sqlizer { + var sep string + if !conf.Server.SearchFullString { + sep = " " + } + q := sanitizeStrings(value) + parts := strings.Split(q, " ") + filters := And{} + for _, part := range parts { + filters = append(filters, Like{"full_text": "%" + sep + part + "%"}) + } + return filters +}