Skip to content

🎧 Your Personal Streaming Service

New option: SearchFullString, to match query strings anywhere in searchable fields, not only in word boundaries

Deluan76e522710a11
diff --git a/conf/configuration.go b/conf/configuration.go
index 0e3a9b4a5..2393db0d5 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 c5208a96d..13a7433ca 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 55c5b8582..21eaa39b2 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...)
}
+ 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 {
- sq = sq.Where(Like{"full_text": "% " + part + "%"})
+ filters = append(filters, Like{"full_text": "%" + sep + part + "%"})
}
- err := r.queryAll(sq, results)
- return err
+ return filters
}