From 819bda855eb1d19a2b578c9cd712b01e50dc3fad Mon Sep 17 00:00:00 2001 From: Rakib Date: Wed, 16 Aug 2023 22:41:44 +0600 Subject: [PATCH] fix query predicate bug for NotEqual expression type (#399) --- .../AggregationPredicates/QueryPredicate.cs | 9 +++++++-- .../RediSearchTests/AggregationFunctionalTests.cs | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs b/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs index 2d3740b4..9e9b235f 100644 --- a/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs +++ b/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs @@ -148,7 +148,7 @@ protected override void SplitBinaryExpression(BinaryExpression expression, Stack } } - private static string BuildEqualityPredicate(MemberInfo member, ConstantExpression expression, string memberStr) + private static string BuildEqualityPredicate(MemberInfo member, ConstantExpression expression, string memberStr, bool negated = false) { var sb = new StringBuilder(); var fieldAttribute = member.GetCustomAttribute(); @@ -157,6 +157,11 @@ private static string BuildEqualityPredicate(MemberInfo member, ConstantExpressi throw new InvalidOperationException("Searches can only be performed on fields marked with a RedisFieldAttribute with the SearchFieldType not set to None"); } + if (negated) + { + sb.Append("-"); + } + sb.Append($"{memberStr}:"); var searchFieldType = fieldAttribute.SearchFieldType != SearchFieldType.INDEXED ? fieldAttribute.SearchFieldType @@ -189,7 +194,7 @@ private string BuildQueryPredicate(ExpressionType expType, MemberExpression memb ExpressionType.GreaterThanOrEqual => $"{memberStr}:[{constExpression.Value} inf]", ExpressionType.LessThanOrEqual => $"{memberStr}:[-inf {constExpression.Value}]", ExpressionType.Equal => BuildEqualityPredicate(member.Member, constExpression, memberStr), - ExpressionType.NotEqual => $"{memberStr} : -{{{constExpression.Value}}}", + ExpressionType.NotEqual => BuildEqualityPredicate(member.Member, constExpression, memberStr, true), _ => string.Empty }; return queryPredicate; diff --git a/test/Redis.OM.Unit.Tests/RediSearchTests/AggregationFunctionalTests.cs b/test/Redis.OM.Unit.Tests/RediSearchTests/AggregationFunctionalTests.cs index f55b12d6..d675c9ba 100644 --- a/test/Redis.OM.Unit.Tests/RediSearchTests/AggregationFunctionalTests.cs +++ b/test/Redis.OM.Unit.Tests/RediSearchTests/AggregationFunctionalTests.cs @@ -271,5 +271,19 @@ public async Task GetGroupCount() Assert.True(1<=result["COUNT"]); } } + + [Fact] + public async Task GetGroupCountWithNegationQuery() + { + Setup(); + var collection = new RedisAggregationSet(_connection); + var results = await collection + .Where(x => x.RecordShell.Age != 0) + .GroupBy(x => x.RecordShell.Age).CountGroupMembers().ToListAsync(); + foreach (var result in results) + { + Assert.True(1 <= result["COUNT"]); + } + } } }