Skip to content

Backport of: Fix failing docuement.load in query results with an expired doc #357 #410

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 1 commit into from
Apr 14, 2025
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
4 changes: 4 additions & 0 deletions src/NRedisStack/Search/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static Document Load(string id, double score, byte[]? payload, RedisValue
{
Document ret = new Document(id, score, payload);
if (fields == null) return ret;
if (fields.Length == 1 && fields[0].IsNull)
{
return ret;
}
for (int i = 0; i < fields.Length; i += 2)
{
string fieldName = fields[i]!;
Expand Down
50 changes: 49 additions & 1 deletion tests/NRedisStack.Tests/Search/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


namespace NRedisStack.Tests.Search;

public class SearchTests : AbstractNRedisStackTest, IDisposable
{
// private readonly string key = "SEARCH_TESTS";
Expand Down Expand Up @@ -3313,4 +3312,53 @@ public void TestNumericLogicalOperatorsInDialect4()
Assert.Equal(1, ft.Search(index, new Query("@version:[123 123] | @id:[456 7890]")).TotalResults);
Assert.Equal(1, ft.Search(index, new Query("@version==123 @id==456").Dialect(4)).TotalResults);
}

[Fact]
public void TestDocumentLoad_Issue352()
{
Document d = Document.Load("1", 0.5, null, new RedisValue[] { RedisValue.Null });
Assert.Empty(d.GetProperties().ToList());
}

[SkipIfRedis(Is.OSSCluster)]
public void TestDocumentLoadWithDB_Issue352()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();

Schema sc = new Schema().AddTextField("first", 1.0).AddTextField("last", 1.0).AddNumericField("age");
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

Document droppedDocument = null;
int numberOfAttempts = 0;
do
{
db.HashSet("student:1111", new HashEntry[] { new("first", "Joe"), new("last", "Dod"), new("age", 18) });

Assert.True(db.KeyExpire("student:1111", TimeSpan.FromMilliseconds(500)));

Boolean cancelled = false;
Task searchTask = Task.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
SearchResult result = ft.Search(index, new Query());
List<Document> docs = result.Documents;
if (docs.Count == 0 || cancelled)
{
break;
}
else if (docs[0].GetProperties().ToList().Count == 0)
{
droppedDocument = docs[0];
}
}
});
Task.WhenAny(searchTask, Task.Delay(1000)).GetAwaiter().GetResult();
Assert.True(searchTask.IsCompleted);
Assert.Null(searchTask.Exception);
cancelled = true;
} while (droppedDocument == null && numberOfAttempts++ < 3);
}
}
Loading