Skip to content

Commit

Permalink
[C] Redo Search in API to Return More Useful Results
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Aug 2, 2018
1 parent b856cf8 commit f0a1f55
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions Shoko.Server/API/v2/Modules/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,10 @@ private static void CheckTagsFuzzy(SVR_AnimeSeries a, string query,
class SearchGrouping
{
public List<SVR_AnimeSeries> Series { get; set; }
public bool exact_match { get; set; }
public int distance { get; set; }
public int index { get; set; }
public string match { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -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<SVR_AnimeSeries, string> ser in series)
{
if (offset == 0)
Expand Down Expand Up @@ -2518,7 +2530,7 @@ internal object Search(string query, int limit, int limit_tag, int offset, int t
var distLevenshtein = new ConcurrentDictionary<SVR_AnimeSeries, Tuple<Misc.SearchInfo, string>>();
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);
Expand Down Expand Up @@ -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<SVR_AnimeSeries, Tuple<Misc.SearchInfo, string>>();

Expand All @@ -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));
Expand Down

0 comments on commit f0a1f55

Please sign in to comment.