From 05c9d7308aa2ef405352271f4254a519c397d317 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 21 Aug 2021 12:05:35 -0500 Subject: exclude terms prefixed with - --- src/slskd/Application/SharedFileCache.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 860bcfa3..5d12ed63 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -275,17 +275,16 @@ namespace slskd /// The matching files. public async Task> 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("'", "''"); - var tokens = text.Split(' ').Select(token => $"\"{token}\""); + 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 '{string.Join(" AND ", tokens)}'"; + var sql = $"SELECT * FROM cache WHERE cache MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}'"; try { -- cgit v1.2.3