Skip to content

Commit

Permalink
Change InfoResult::Attributes Returned Value to Array of Arrays (#295)
Browse files Browse the repository at this point in the history
* Change InfoResult/Attribute to Array of Arrays

* test with and without sortable

* remove GetRedisResultDictionaryArray

* print connection data

* add is enterpeise and oss cluster to printing

* delete last commit

* effort to keep "InfoResult" backward compatible

* remove sortable attribute

* remove sortable

---------

Co-authored-by: atakavci <[email protected]>
Co-authored-by: atakavci <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent a2b2524 commit 5679194
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 14 deletions.
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

0 comments on commit 5679194

Please sign in to comment.