Skip to content

Commit

Permalink
Creates Ranked types for weighting
Browse files Browse the repository at this point in the history
`.ToRanked()` no takes an enum to determine the type of ranking to perform
  • Loading branch information
ninjanye committed Sep 19, 2016
1 parent c3cbf47 commit 6142d03
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
6 changes: 2 additions & 4 deletions NinjaNye.SearchExtensions.Tests/Fluent/FluentRankedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,18 @@ public void ToRanked_SearchedForData_RankedResultIsReturned()
[Test]
public void ToRanked_CorrectRankReturned()
{
var result = _testData.Search(x => x.Name).ContainingAll("wee").ToLeftWeightedRanked();
var result = _testData.Search(x => x.Name).ContainingAll("wee").ToRanked(RankedType.LeftWeighted);
var first = result.OrderByDescending(r => r.Hits).First();
var check = first.Hits;
//as 'wee' is one char into string, it should add (7 - 1) to the hit count. - should add 6
Assert.AreEqual(7, first.Hits);

result = _testData.Search(x => x.Name).ContainingAll("ete").ToLeftWeightedRanked();
result = _testData.Search(x => x.Name).ContainingAll("ete").ToRanked(RankedType.LeftWeighted);
first = result.OrderByDescending(r => r.Hits).First();

//as 'ete' is three char into string, it should add (7 - 3) to the hit count. - should add 4
Assert.AreEqual(5, first.Hits);
}


[Test]
public void ToRanked_SearchOneColumn_RankIsCorrect()
{
Expand Down
26 changes: 3 additions & 23 deletions NinjaNye.SearchExtensions/EnumerableStringSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public IEnumerable<T> ReverseSoundex(params string[] terms)
/// Enumerable of ranked items. Each item will contain
/// the amount of hits found across the defined properties
/// </returns>
public IEnumerable<IRanked<T>> ToRanked()
public IEnumerable<IRanked<T>> ToRanked(RankedType type = RankedType.Default)
{
Expression combinedHitExpression = null;
foreach (var propertyToSearch in Properties)
Expand All @@ -253,7 +253,8 @@ public IEnumerable<IRanked<T>> ToRanked()
{
var searchTerm = _searchTerms[j];
var nullSafeExpresion = BuildNullSafeExpresion(propertyToSearch);
var hitCountExpression = EnumerableExpressionHelper.CalculateHitCount(nullSafeExpresion, searchTerm, _searchOptions);
var hitCountExpression = type == RankedType.Default ? EnumerableExpressionHelper.CalculateHitCount(nullSafeExpresion, searchTerm, _searchOptions)
: EnumerableExpressionHelper.CalculateHitCount_LeftWeighted(nullSafeExpresion, searchTerm, _searchOptions);
combinedHitExpression = ExpressionHelper.AddExpressions(combinedHitExpression, hitCountExpression);
}
}
Expand All @@ -263,27 +264,6 @@ public IEnumerable<IRanked<T>> ToRanked()
return this.Select(selectExpression.Invoke);
}

public IEnumerable<IRanked<T>> ToLeftWeightedRanked()
{
Expression combinedHitExpression = null;
foreach (var propertyToSearch in Properties)
{
for (int j = 0; j < _searchTerms.Count; j++)
{
var searchTerm = _searchTerms[j];
var nullSafeExpresion = BuildNullSafeExpresion(propertyToSearch);
var hitCountExpression = EnumerableExpressionHelper.CalculateHitCount_LeftWeighted(nullSafeExpresion, searchTerm, _searchOptions);
combinedHitExpression = ExpressionHelper.AddExpressions(combinedHitExpression, hitCountExpression);
}
}

var rankedInitExpression = EnumerableExpressionHelper.ConstructRankedResult<T>(combinedHitExpression, FirstParameter);
var selectExpression = Expression.Lambda<Func<T, Ranked<T>>>(rankedInitExpression, FirstParameter).Compile();
return this.Select(selectExpression.Invoke);
}



private Expression<Func<T, string>> BuildNullSafeExpresion(Expression<Func<T, string>> propertyToSearch)
{
var nullSafeProperty = Expression.Coalesce(propertyToSearch.Body, ExpressionMethods.EmptyStringExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ public static Expression CalculateHitCount_LeftWeighted<T>(Expression<Func<T, st

var finalHitCounterExpressionOffset = Expression.Add(hitCountExpression, leftWeightExpression);
return finalHitCounterExpressionOffset;

// return hitCountExpression;
}



/// <summary>
/// Calculates how many search hits occured for a given property
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions NinjaNye.SearchExtensions/RankedType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NinjaNye.SearchExtensions
{
public enum RankedType
{
Default,
LeftWeighted
}
}

0 comments on commit 6142d03

Please sign in to comment.