Skip to content

fixing paths to nested objects in various predicates #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2023
Merged
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
8 changes: 4 additions & 4 deletions src/Redis.OM/Common/ExpressionTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static string GetFieldName(Expression exp)

if (exp is MemberExpression member)
{
return $"{member.Member.Name}";
return ExpressionParserUtilities.GetSearchFieldNameFromMember(member);
}

if (exp is MethodCallExpression method)
Expand Down Expand Up @@ -411,7 +411,7 @@ private static string[] GetFieldNamesForExpression(Expression exp)

if (exp is MemberExpression member)
{
return new[] { $"{member.Member.Name}" };
return new[] { ExpressionParserUtilities.GetSearchFieldNameFromMember(member) };
}

if (exp is MethodCallExpression method)
Expand All @@ -431,7 +431,7 @@ private static string[] GetFieldNamesForExpression(Expression exp)

if (exp is NewExpression newExpression)
{
return newExpression.Members != null ? newExpression.Members.Select(x => $"{x.Name}").ToArray() : Array.Empty<string>();
return newExpression.Members != null ? newExpression.Arguments.Select(GetFieldName).ToArray() : Array.Empty<string>();
}

throw new ArgumentException("Invalid expression type detected");
Expand Down Expand Up @@ -582,7 +582,7 @@ private static RedisSortBy TranslateOrderByMethod(MethodCallExpression expressio
var predicate = (UnaryExpression)expression.Arguments[1];
var lambda = (LambdaExpression)predicate.Operand;
var memberExpression = (MemberExpression)lambda.Body;
sb.Field = memberExpression.Member.Name;
sb.Field = ExpressionParserUtilities.GetSearchFieldNameFromMember(memberExpression);
sb.Direction = ascending ? SortDirection.Ascending : SortDirection.Descending;
return sb;
}
Expand Down
40 changes: 40 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/AggregationSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,45 @@ public void TestMultipleOrderBys()
_ = collection.OrderBy(x => x.RecordShell.Name).OrderByDescending(x => x.RecordShell.Age).ToList();
_mock.Verify(x=>x.Execute("FT.AGGREGATE","person-idx", "*", "SORTBY", "4", "@Name", "ASC", "@Age", "DESC", "WITHCURSOR", "COUNT", "10000"));
}

[Fact]
public void TestNestedOrderBy()
{
var collection = new RedisAggregationSet<Person>(_mock.Object, true, chunkSize: 10000);
_mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny<string[]>())).Returns(MockedResult);
_mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny<string[]>())).Returns(MockedResultCursorEnd);
_ = collection.OrderBy(x => x.RecordShell.Address.State).ToList();
_mock.Verify(x=>x.Execute("FT.AGGREGATE","person-idx", "*", "SORTBY", "2", "@Address_State", "ASC", "WITHCURSOR", "COUNT", "10000"));
}

[Fact]
public void TestNestedGroup()
{
var collection = new RedisAggregationSet<Person>(_mock.Object, true, chunkSize: 10000);
_mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny<string[]>())).Returns(MockedResult);
_mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny<string[]>())).Returns(MockedResultCursorEnd);
_ = collection.GroupBy(x => x.RecordShell.Address.State).ToList();
_mock.Verify(x=>x.Execute("FT.AGGREGATE","person-idx", "*", "GROUPBY", "1", "@Address_State", "WITHCURSOR", "COUNT", "10000"));
}

[Fact]
public void TestNestedGroupMulti()
{
var collection = new RedisAggregationSet<Person>(_mock.Object, true, chunkSize: 10000);
_mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny<string[]>())).Returns(MockedResult);
_mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny<string[]>())).Returns(MockedResultCursorEnd);
_ = collection.GroupBy(x => new {x.RecordShell.Address.State, x.RecordShell.Address.ForwardingAddress.City}).ToList();
_mock.Verify(x=>x.Execute("FT.AGGREGATE","person-idx", "*", "GROUPBY", "2", "@Address_State", "@Address_ForwardingAddress_City", "WITHCURSOR", "COUNT", "10000"));
}

[Fact]
public void TestNestedApply()
{
var collection = new RedisAggregationSet<Person>(_mock.Object, true, chunkSize: 10000);
_mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny<string[]>())).Returns(MockedResult);
_mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny<string[]>())).Returns(MockedResultCursorEnd);
_ = collection.Apply(x => x.RecordShell.Address.HouseNumber + 4, "house_num_modified").ToList();
_mock.Verify(x=>x.Execute("FT.AGGREGATE","person-idx", "*", "APPLY", "@Address_HouseNumber + 4", "AS", "house_num_modified", "WITHCURSOR", "COUNT", "10000"));
}
}
}
34 changes: 34 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2816,5 +2816,39 @@ public void SearchNumericEnumFieldContainsListAsProperty()
"0",
"100"));
}

[Fact]
public void TestNestedOrderBy()
{
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(_mockReply);
var collection = new RedisCollection<Person>(_mock.Object).OrderBy(x => x.Address.State).ToList();
_mock.Verify(x=>x.Execute("FT.SEARCH", "person-idx", "*", "LIMIT","0", "100", "SORTBY", "Address_State", "ASC"));
}

[Fact]
public void TestGeoFilterNested()
{
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(_mockReply);

var collection = new RedisCollection<Person>(_mock.Object);
var res = collection.GeoFilter(x => x.Address.Location, 5, 6.7, 50, GeoLocDistanceUnit.Kilometers).ToList();
Assert.Equal(32, res[0].Age);
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"person-idx",
"*",
"LIMIT",
"0",
"100",
"GEOFILTER",
"Address_Location",
"5",
"6.7",
"50",
"km"
));
}
}
}