Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,26 @@ private static string TranslateMatchContains(MethodCallExpression exp)
{
var source = GetOperandString(exp.Arguments[0]);
var infix = GetOperandString(exp.Arguments[1]);
return $"({source}:*{infix}*)";
if (exp.Arguments[0].Type == typeof(string))
{
return $"({source}:*{infix}*)";
}

// IEnumerable<string>
return $"({source}:{{*{infix}*}})";
}

private static string TranslateMatchPattern(MethodCallExpression exp)
{
var source = GetOperandString(exp.Arguments[0]);
var pattern = GetOperandString(exp.Arguments[1]);
return $"({source}:{pattern})";
if (exp.Arguments[0].Type == typeof(string))
{
return $"({source}:{pattern})";
}

// IEnumerable<string>
return $"({source}:{{{pattern}}})";
}

private static string TranslateFuzzyMatch(MethodCallExpression exp)
Expand Down
28 changes: 28 additions & 0 deletions src/Redis.OM/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ public static bool MatchPattern(this string source, string pattern)
return terms.Any(t => t.EndsWith(pattern));
}

/// <summary>
/// Checks the source string to see if any tokens within the source contains the infix.
/// </summary>
/// <param name="source">The string to check.</param>
/// <param name="infix">The infix to look for within the string.</param>
/// <returns>Whether any token within the source string contains the infix.</returns>
/// <remarks>This is meant to be a shadow method that runs within an expression, a working implementation is
/// provided here for completeness.</remarks>
public static bool MatchContains(this IEnumerable<string> source, string infix)
{
var terms = string.Join(" ", source).Split(SplitChars);
return terms.Any(t => t.Contains(infix));
}

/// <summary>
/// Checks the source string to see if any tokens within the source matches the pattern.
/// </summary>
/// <param name="source">The string to check.</param>
/// <param name="pattern">The pattern to look for within the string.</param>
/// <returns>Whether any token within the source string matches the pattern.</returns>
/// <remarks>This is meant to be a shadow method that runs within an expression, a working implementation is
/// provided here for completeness.</remarks>
public static bool MatchPattern(this IEnumerable<string> source, string pattern)
{
var terms = string.Join(" ", source).Split(SplitChars);
return terms.Any(t => t.EndsWith(pattern));
}

/// <summary>
/// Wagner-Fischer dynamic programming string distance algorithm.
/// </summary>
Expand Down
41 changes: 38 additions & 3 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public void TestMatchEndsWith()
}

[Fact]
public void TestMatchContains()
public void TestMatchContainsOfString()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);
Expand All @@ -478,7 +478,24 @@ public void TestMatchContains()
"0",
"100");
}


[Fact]
public void TestMatchContainsOfStringArray()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var collection = new RedisCollection<Person>(_substitute);
_ = collection.Where(x => x.NickNames.MatchContains("Ste")).ToList();
_substitute.Received().Execute(
"FT.SEARCH",
"person-idx",
"(@NickNames:{*Ste*})",
"LIMIT",
"0",
"100");
}

[Fact]
public void TestMultipleMatches()
{
Expand All @@ -497,7 +514,7 @@ public void TestMultipleMatches()
}

[Fact]
public void TestMatchPattern()
public void TestMatchPatternOfString()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);
Expand All @@ -514,6 +531,24 @@ public void TestMatchPattern()
"100");
}

[Fact]
public void TestMatchPatternOfStringArray()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var collection = new RedisCollection<Person>(_substitute);
var ddfgdf = collection.Where(x => x.NickNames.MatchPattern("Ste* Lo*")).ToList();

_substitute.Received().Execute(
"FT.SEARCH",
"person-idx",
"(@NickNames:{Ste* Lo*})",
"LIMIT",
"0",
"100");
}

[Fact]
public void TestTagContains()
{
Expand Down