aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-08-22 21:55:26 -0500
committerGitHub <noreply@github.com>2021-08-22 21:55:26 -0500
commitcd564c0df2fd55803a193efe3fa67f850426406f (patch)
treec1194f8b6d041b51b16cce107aba6c91ce1865a3 /src/slskd
parent1bb9234355c1193caf07e7cc5d994dc6e99b9222 (diff)
parente16883c11f6254635328c5e0d058273f9b33aa6c (diff)
downloadslskd-cd564c0df2fd55803a193efe3fa67f850426406f.tar.gz
Merge pull request #159 from slskd/search-matches
Improve search matching
Diffstat (limited to 'src/slskd')
-rw-r--r--src/slskd/Application/SharedFileCache.cs14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs
index 5b2e2309..049d80fe 100644
--- a/src/slskd/Application/SharedFileCache.cs
+++ b/src/slskd/Application/SharedFileCache.cs
@@ -275,14 +275,16 @@ namespace slskd
/// <returns>The matching files.</returns>
public async Task<IEnumerable<File>> SearchAsync(SearchQuery query)
{
- // sanitize the query string. there's probably more to it than this.
- var text = query.Query
- .Replace("/", " ")
+ string Clean(string str) => str.Replace("/", " ")
.Replace("\\", " ")
.Replace(":", " ")
- .Replace("\"", " ");
+ .Replace("\"", " ")
+ .Replace("'", "''");
- var sql = $"SELECT * FROM cache WHERE cache MATCH '\"{text.Replace("'", "''")}\"'";
+ var match = string.Join(" AND ", query.Terms.Select(token => $"\"{Clean(token)}\""));
+ var exclusions = string.Join(" OR ", query.Exclusions.Select(exclusion => $"\"{Clean(exclusion)}\""));
+
+ var sql = $"SELECT * FROM cache WHERE cache MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}'";
try
{
@@ -295,6 +297,8 @@ namespace slskd
results.Add(reader.GetString(0));
}
+ Log.Debug($"Query {sql} returned {results.Count} results");
+
return results.Select(r => MaskedFiles[r.Replace("''", "'")]);
}
catch (Exception ex)