Skip to content

Commit

Permalink
crossed wires between inverted contains and infix (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 authored Oct 26, 2023
1 parent 13b0143 commit fa80a84
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,21 @@ private static string TranslateContainsStandardQuerySyntax(MethodCallExpression
}

type = Nullable.GetUnderlyingType(propertyExpression.Type) ?? propertyExpression.Type;
var valueType = Nullable.GetUnderlyingType(valuesExpression.Type) ?? valuesExpression.Type;
memberName = GetOperandStringForMember(propertyExpression);
var treatEnumsAsInts = type.IsEnum && !(propertyExpression.Member.GetCustomAttributes(typeof(JsonConverterAttribute)).FirstOrDefault() is JsonConverterAttribute converter && converter.ConverterType == typeof(JsonStringEnumConverter));
literal = GetOperandStringForQueryArgs(valuesExpression, treatEnumsAsInts);

if ((type == typeof(string) || type == typeof(string[]) || type == typeof(List<string>) || type == typeof(Guid) || type == typeof(Guid[]) || type == typeof(List<Guid>) || type == typeof(Ulid) || (type.IsEnum && !treatEnumsAsInts)) && attribute is IndexedAttribute)
if ((valueType == typeof(List<string>) || valueType == typeof(string[]) || type == typeof(string[]) || type == typeof(List<string>) || type == typeof(Guid) || type == typeof(Guid[]) || type == typeof(List<Guid>) || type == typeof(Guid[]) || type == typeof(List<Guid>) || type == typeof(Ulid) || (type.IsEnum && !treatEnumsAsInts)) && attribute is IndexedAttribute)
{
return $"({memberName}:{{{EscapeTagField(literal).Replace("\\|", "|")}}})";
}

if (type == typeof(string) && attribute is IndexedAttribute)
{
return $"({memberName}:{{*{EscapeTagField(literal)}*}})";
}

if (type == typeof(string) && attribute is SearchableAttribute)
{
return $"({memberName}:{literal})";
Expand Down
30 changes: 29 additions & 1 deletion test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void TestMatchEndsWith()
"0",
"100");
}

[Fact]
public void TestMatchContains()
{
Expand All @@ -390,6 +390,34 @@ public void TestMatchContains()
"100");
}

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

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

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

[Fact]
public void TestTagStartsWith()
{
Expand Down

0 comments on commit fa80a84

Please sign in to comment.