From f0a1f556e394027e47a68675145da576b41f1221 Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Thu, 2 Aug 2018 01:41:15 -0400 Subject: [PATCH] [C] Redo Search in API to Return More Useful Results --- Shoko.Server/API/v2/Modules/Common.cs | 36 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/Shoko.Server/API/v2/Modules/Common.cs b/Shoko.Server/API/v2/Modules/Common.cs index 8b4310b59..5251f0e00 100644 --- a/Shoko.Server/API/v2/Modules/Common.cs +++ b/Shoko.Server/API/v2/Modules/Common.cs @@ -2327,6 +2327,10 @@ private static void CheckTagsFuzzy(SVR_AnimeSeries a, string query, class SearchGrouping { public List Series { get; set; } + public bool exact_match { get; set; } + public int distance { get; set; } + public int index { get; set; } + public string match { get; set; } } /// @@ -2457,11 +2461,19 @@ internal object Search(string query, int limit, int limit_tag, int offset, int t if (title2 == null) return -1; return String.Compare(title1, title2, StringComparison.InvariantCultureIgnoreCase); }); - return Tuple.Create(a.Key, tempSeries); + var result = new SearchGrouping + { + Series = a.OrderBy(b => b.AirDate).ToList(), + exact_match = distLevenshtein[tempSeries[0]].Item1.exact_match, + distance = distLevenshtein[tempSeries[0]].Item1.distance, + index = distLevenshtein[tempSeries[0]].Item1.index, + match = distLevenshtein[tempSeries[0]].Item2 + }; + return result; }); - series = tempListToSort.OrderBy(a => distLevenshtein[a.Item2[0]].Item1.distance) - .SelectMany(a => a.Item2).ToDictionary(a => a, a => distLevenshtein[a].Item2); + series = tempListToSort.OrderBy(a => a.distance) + .SelectMany(a => a.Series).ToDictionary(a => a, a => distLevenshtein[a].Item2); foreach (KeyValuePair ser in series) { if (offset == 0) @@ -2518,7 +2530,7 @@ internal object Search(string query, int limit, int limit_tag, int offset, int t var distLevenshtein = new ConcurrentDictionary>(); allSeries.ForAll(a => CheckTagsFuzzy(a, query, ref distLevenshtein, realLimit)); - series = distLevenshtein.Keys.OrderBy(a => distLevenshtein[a].Item1) + series = distLevenshtein.Keys.OrderBy(a => distLevenshtein[a].Item1.distance) .ThenBy(a => distLevenshtein[a].Item2.Length) .ThenBy(a => a.Contract.AniDBAnime.AniDBAnime.MainTitle) .ToDictionary(a => a, a => distLevenshtein[a].Item2); @@ -2649,11 +2661,19 @@ internal object Search(string query, int limit, int limit_tag, int offset, int t if (title2 == null) return -1; return String.Compare(title1, title2, StringComparison.InvariantCultureIgnoreCase); }); - return Tuple.Create(a.Key, tempSeries); + var result = new SearchGrouping + { + Series = a.OrderBy(b => b.AirDate).ToList(), + exact_match = distLevenshtein[tempSeries[0]].Item1.exact_match, + distance = distLevenshtein[tempSeries[0]].Item1.distance, + index = distLevenshtein[tempSeries[0]].Item1.index, + match = distLevenshtein[tempSeries[0]].Item2 + }; + return result; }); - series = tempListToSort.OrderBy(a => distLevenshtein[a.Item2[0]].Item1.distance) - .SelectMany(a => a.Item2).ToDictionary(a => a, a => distLevenshtein[a].Item2); + series = tempListToSort.OrderBy(a => a.distance) + .SelectMany(a => a.Series).ToDictionary(a => a, a => distLevenshtein[a].Item2); distLevenshtein = new ConcurrentDictionary>(); @@ -2663,7 +2683,7 @@ internal object Search(string query, int limit, int limit_tag, int offset, int t if (tag_limit > 0) { allSeries.ForAll(a => CheckTagsFuzzy(a, query, ref distLevenshtein, tag_limit)); - series.AddRange(distLevenshtein.Keys.OrderBy(a => distLevenshtein[a].Item1) + series.AddRange(distLevenshtein.Keys.OrderBy(a => distLevenshtein[a].Item1.distance) .ThenBy(a => distLevenshtein[a].Item2.Length) .ThenBy(a => a.Contract.AniDBAnime.AniDBAnime.MainTitle) .ToDictionary(a => a, a => distLevenshtein[a].Item2));