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

Based on feedback from @orlea, in https://github.com/deluan/navidrome/issues/255#issuecomment-683427754
This commit is contained in:
Deluan 2020-08-30 13:08:10 -04:00
parent aae9d89e8c
commit 76e522710a
3 changed files with 21 additions and 12 deletions

View File

@ -28,6 +28,7 @@ type configOptions struct {
ImageCacheSize string ImageCacheSize string
AutoImportPlaylists bool AutoImportPlaylists bool
SearchFullString bool
IgnoredArticles string IgnoredArticles string
IndexGroups string IndexGroups string
ProbeCommand string ProbeCommand string
@ -87,6 +88,7 @@ func init() {
viper.SetDefault("autoimportplaylists", true) viper.SetDefault("autoimportplaylists", true)
// Config options only valid for file/env configuration // 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("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("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") viper.SetDefault("probecommand", "ffmpeg %s -f ffmetadata")

View File

@ -62,11 +62,5 @@ func booleanFilter(field string, value interface{}) Sqlizer {
} }
func fullTextFilter(field string, value interface{}) Sqlizer { func fullTextFilter(field string, value interface{}) Sqlizer {
q := sanitizeStrings(value.(string)) return fullTextExpr(value.(string))
parts := strings.Split(q, " ")
filters := And{}
for _, part := range parts {
filters = append(filters, Like{"full_text": "% " + part + "%"})
}
return filters
} }

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
. "github.com/Masterminds/squirrel" . "github.com/Masterminds/squirrel"
"github.com/deluan/navidrome/conf"
"github.com/kennygrant/sanitize" "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 { func (r sqlRepository) doSearch(q string, offset, size int, results interface{}, orderBys ...string) error {
q = strings.TrimSpace(q)
q = strings.TrimSuffix(q, "*") q = strings.TrimSuffix(q, "*")
q = sanitizeStrings(q)
if len(q) < 2 { if len(q) < 2 {
return nil return nil
} }
sq := r.newSelectWithAnnotation(r.tableName + ".id").Columns("*") sq := r.newSelectWithAnnotation(r.tableName + ".id").Columns("*")
sq = sq.Limit(uint64(size)).Offset(uint64(offset)) sq = sq.Limit(uint64(size)).Offset(uint64(offset))
if len(orderBys) > 0 { if len(orderBys) > 0 {
sq = sq.OrderBy(orderBys...) sq = sq.OrderBy(orderBys...)
} }
parts := strings.Split(q, " ") sq = sq.Where(fullTextExpr(q))
for _, part := range parts {
sq = sq.Where(Like{"full_text": "% " + part + "%"})
}
err := r.queryAll(sq, results) err := r.queryAll(sq, results)
return err 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
}