Skip to content
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

Change InfoResult::Attributes Returned Value to Array of Arrays #295

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
13 changes: 9 additions & 4 deletions src/NRedisStack/Search/DataTypes/InfoResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace NRedisStack.Search.DataTypes;
public class InfoResult
{
private readonly Dictionary<string, RedisResult> _all = new();
private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE" };
public string IndexName => GetString("index_name")!;
public Dictionary<string, RedisResult> IndexOption => GetRedisResultDictionary("index_options")!;
public Dictionary<string, RedisResult>[] Attributes => GetRedisResultDictionaryArray("attributes")!;
Expand Down Expand Up @@ -91,7 +92,6 @@ private double GetDouble(string key)
}

return result;

}

private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key)
Expand All @@ -105,12 +105,17 @@ private double GetDouble(string key)
var dict = new Dictionary<string, RedisResult>();
for (int j = 0; j < fv.Length; j += 2)
{
dict.Add((string)fv[j]!, fv[j + 1]);
if (booleanAttributes.Contains((string)fv[j]!))
{
dict.Add((string)fv[j]!, fv[j--]);
}
else
{
dict.Add((string)fv[j]!, fv[j + 1]);
}
}

result[i] = dict;
}

return result;
}
}
6 changes: 3 additions & 3 deletions tests/NRedisStack.Tests/Examples/ExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ public void AdvancedQueryOperationsTest()
}
catch
{
//Todo: Check When Exception Catch
//Todo: Check When Exception Catch
}

Assert.True(ft.Create("vss_idx", new FTCreateParams().On(IndexDataType.HASH).Prefix("vec:"),
Expand Down Expand Up @@ -1329,7 +1329,7 @@ public void AdvancedQueryOperationsTest()
}
catch
{
//Todo: Check When Exception Catch
//Todo: Check When Exception Catch
}

Assert.True(ft.Create("wh_idx", new FTCreateParams()
Expand Down Expand Up @@ -1442,4 +1442,4 @@ private static void SortAndCompare(List<string> expectedList, List<string> res)
Assert.Equal(expectedList[i], res[i].ToString());
}
}
}
}
141 changes: 134 additions & 7 deletions tests/NRedisStack.Tests/Search/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public void AlterAdd()
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true, unf: true);
Schema sc = new Schema().AddTextField("title", 1.0);

Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

Expand Down Expand Up @@ -733,9 +733,10 @@ public void AlterAdd()
Assert.Equal(index, info.IndexName);
Assert.Empty(info.IndexOption);
// Assert.Equal(,info.IndexDefinition);
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());

Assert.Equal(100, info.NumDocs);
Assert.NotNull(info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Expand Down Expand Up @@ -795,9 +796,135 @@ public async Task AlterAddAsync()

var info = await ft.InfoAsync(index);
Assert.Equal(index, info.IndexName);
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
Assert.Equal(100, info.NumDocs);
Assert.Equal("300", info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Assert.True(info.NumRecords >= 200);
Assert.True(info.InvertedSzMebibytes < 1); // TODO: check this line and all the <1 lines
Assert.Equal(0, info.VectorIndexSzMebibytes);
Assert.Equal(208, info.TotalInvertedIndexBlocks);
Assert.True(info.OffsetVectorsSzMebibytes < 1);
Assert.True(info.DocTableSizeMebibytes < 1);
Assert.Equal(0, info.SortableValueSizeMebibytes);
Assert.True(info.KeyTableSizeMebibytes < 1);
Assert.Equal(8, (int)info.RecordsPerDocAvg);
Assert.True(info.BytesPerRecordAvg > 5);
Assert.True(info.OffsetsPerTermAvg > 0.8);
Assert.Equal(8, info.OffsetBitsPerRecordAvg);
Assert.Equal(0, info.HashIndexingFailures);
Assert.True(info.TotalIndexingTime > 0);
Assert.Equal(0, info.Indexing);
Assert.Equal(1, info.PercentIndexed);
Assert.Equal(4, info.NumberOfUses);
Assert.Equal(7, info.GcStats.Count);
Assert.Equal(4, info.CursorStats.Count);
}

[SkipIfRedis(Is.OSSCluster, Is.Enterprise)]
public void AlterAddSortable()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);

Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

//sleep:
System.Threading.Thread.Sleep(2000);

var fields = new HashEntry("title", "hello world");
//fields.("title", "hello world");
for (int i = 0; i < 100; i++)
{
db.HashSet($"doc{i}", fields.Name, fields.Value);
}
SearchResult res = ft.Search(index, new Query("hello world"));
Assert.Equal(100, res.TotalResults);

Assert.True(ft.Alter(index, new Schema().AddTagField("tags").AddTextField("name", weight: 0.5)));
for (int i = 0; i < 100; i++)
{
var fields2 = new HashEntry[] { new("name", "name" + i),
new("tags", $"tagA,tagB,tag{i}") };
// assertTrue(client.updateDocument(string.format("doc%d", i), 1.0, fields2));
db.HashSet($"doc{i}", fields2);
}
SearchResult res2 = ft.Search(index, new Query("@tags:{tagA}"));
Assert.Equal(100, res2.TotalResults);

var info = ft.Info(index);
Assert.Equal(index, info.IndexName);
Assert.Empty(info.IndexOption);
// Assert.Equal(,info.IndexDefinition);
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
Assert.Equal(100, info.NumDocs);
Assert.NotNull(info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Assert.True(info.NumRecords >= 200);
Assert.True(info.InvertedSzMebibytes < 1); // TODO: check this line and all the <1 lines
Assert.Equal(0, info.VectorIndexSzMebibytes);
Assert.Equal(208, info.TotalInvertedIndexBlocks);
Assert.True(info.OffsetVectorsSzMebibytes < 1);
Assert.True(info.DocTableSizeMebibytes < 1);
Assert.Equal(0, info.SortableValueSizeMebibytes);
Assert.True(info.KeyTableSizeMebibytes < 1);
Assert.Equal(8, (int)info.RecordsPerDocAvg);
Assert.True(info.BytesPerRecordAvg > 5);
Assert.True(info.OffsetsPerTermAvg > 0.8);
Assert.Equal(8, info.OffsetBitsPerRecordAvg);
Assert.Equal(0, info.HashIndexingFailures);
Assert.True(info.TotalIndexingTime > 0);
Assert.Equal(0, info.Indexing);
Assert.Equal(1, info.PercentIndexed);
Assert.Equal(4, info.NumberOfUses);
Assert.Equal(7, info.GcStats.Count);
Assert.Equal(4, info.CursorStats.Count);
}

[SkipIfRedis(Is.OSSCluster, Is.Enterprise)]
public async Task AlterAddSortableAsync()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);

Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

//sleep:
System.Threading.Thread.Sleep(2000);

var fields = new HashEntry("title", "hello world");
//fields.("title", "hello world");
for (int i = 0; i < 100; i++)
{
db.HashSet($"doc{i}", fields.Name, fields.Value);
}
SearchResult res = ft.Search(index, new Query("hello world"));
Assert.Equal(100, res.TotalResults);

Assert.True(await ft.AlterAsync(index, new Schema().AddTagField("tags").AddTextField("name", weight: 0.5)));
for (int i = 0; i < 100; i++)
{
var fields2 = new HashEntry[] { new("name", "name" + i),
new("tags", $"tagA,tagB,tag{i}") };
// assertTrue(client.updateDocument(string.format("doc%d", i), 1.0, fields2));
db.HashSet($"doc{i}", fields2);
}
SearchResult res2 = ft.Search(index, new Query("@tags:{tagA}"));
Assert.Equal(100, res2.TotalResults);

var info = await ft.InfoAsync(index);
Assert.Equal(index, info.IndexName);
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
Assert.Equal(100, info.NumDocs);
Assert.Equal("300", info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Expand Down
Loading