Skip to content

Commit 5679194

Browse files
Change InfoResult::Attributes Returned Value to Array of Arrays (#295)
* 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]>
1 parent a2b2524 commit 5679194

File tree

3 files changed

+146
-14
lines changed

3 files changed

+146
-14
lines changed

src/NRedisStack/Search/DataTypes/InfoResult.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace NRedisStack.Search.DataTypes;
55
public class InfoResult
66
{
77
private readonly Dictionary<string, RedisResult> _all = new();
8+
private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE" };
89
public string IndexName => GetString("index_name")!;
910
public Dictionary<string, RedisResult> IndexOption => GetRedisResultDictionary("index_options")!;
1011
public Dictionary<string, RedisResult>[] Attributes => GetRedisResultDictionaryArray("attributes")!;
@@ -91,7 +92,6 @@ private double GetDouble(string key)
9192
}
9293

9394
return result;
94-
9595
}
9696

9797
private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key)
@@ -105,12 +105,17 @@ private double GetDouble(string key)
105105
var dict = new Dictionary<string, RedisResult>();
106106
for (int j = 0; j < fv.Length; j += 2)
107107
{
108-
dict.Add((string)fv[j]!, fv[j + 1]);
108+
if (booleanAttributes.Contains((string)fv[j]!))
109+
{
110+
dict.Add((string)fv[j]!, fv[j--]);
111+
}
112+
else
113+
{
114+
dict.Add((string)fv[j]!, fv[j + 1]);
115+
}
109116
}
110-
111117
result[i] = dict;
112118
}
113-
114119
return result;
115120
}
116121
}

tests/NRedisStack.Tests/Examples/ExampleTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ public void AdvancedQueryOperationsTest()
11841184
}
11851185
catch
11861186
{
1187-
//Todo: Check When Exception Catch
1187+
//Todo: Check When Exception Catch
11881188
}
11891189

11901190
Assert.True(ft.Create("vss_idx", new FTCreateParams().On(IndexDataType.HASH).Prefix("vec:"),
@@ -1329,7 +1329,7 @@ public void AdvancedQueryOperationsTest()
13291329
}
13301330
catch
13311331
{
1332-
//Todo: Check When Exception Catch
1332+
//Todo: Check When Exception Catch
13331333
}
13341334

13351335
Assert.True(ft.Create("wh_idx", new FTCreateParams()
@@ -1442,4 +1442,4 @@ private static void SortAndCompare(List<string> expectedList, List<string> res)
14421442
Assert.Equal(expectedList[i], res[i].ToString());
14431443
}
14441444
}
1445-
}
1445+
}

tests/NRedisStack.Tests/Search/SearchTests.cs

+134-7
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public void AlterAdd()
702702
IDatabase db = redisFixture.Redis.GetDatabase();
703703
db.Execute("FLUSHALL");
704704
var ft = db.FT();
705-
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true, unf: true);
705+
Schema sc = new Schema().AddTextField("title", 1.0);
706706

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

@@ -733,9 +733,10 @@ public void AlterAdd()
733733
Assert.Equal(index, info.IndexName);
734734
Assert.Empty(info.IndexOption);
735735
// Assert.Equal(,info.IndexDefinition);
736-
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
737-
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
738-
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
736+
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
737+
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
738+
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
739+
739740
Assert.Equal(100, info.NumDocs);
740741
Assert.NotNull(info.MaxDocId);
741742
Assert.Equal(102, info.NumTerms);
@@ -795,9 +796,135 @@ public async Task AlterAddAsync()
795796

796797
var info = await ft.InfoAsync(index);
797798
Assert.Equal(index, info.IndexName);
798-
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
799-
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
800-
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
799+
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
800+
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
801+
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
802+
Assert.Equal(100, info.NumDocs);
803+
Assert.Equal("300", info.MaxDocId);
804+
Assert.Equal(102, info.NumTerms);
805+
Assert.True(info.NumRecords >= 200);
806+
Assert.True(info.InvertedSzMebibytes < 1); // TODO: check this line and all the <1 lines
807+
Assert.Equal(0, info.VectorIndexSzMebibytes);
808+
Assert.Equal(208, info.TotalInvertedIndexBlocks);
809+
Assert.True(info.OffsetVectorsSzMebibytes < 1);
810+
Assert.True(info.DocTableSizeMebibytes < 1);
811+
Assert.Equal(0, info.SortableValueSizeMebibytes);
812+
Assert.True(info.KeyTableSizeMebibytes < 1);
813+
Assert.Equal(8, (int)info.RecordsPerDocAvg);
814+
Assert.True(info.BytesPerRecordAvg > 5);
815+
Assert.True(info.OffsetsPerTermAvg > 0.8);
816+
Assert.Equal(8, info.OffsetBitsPerRecordAvg);
817+
Assert.Equal(0, info.HashIndexingFailures);
818+
Assert.True(info.TotalIndexingTime > 0);
819+
Assert.Equal(0, info.Indexing);
820+
Assert.Equal(1, info.PercentIndexed);
821+
Assert.Equal(4, info.NumberOfUses);
822+
Assert.Equal(7, info.GcStats.Count);
823+
Assert.Equal(4, info.CursorStats.Count);
824+
}
825+
826+
[SkipIfRedis(Is.OSSCluster, Is.Enterprise)]
827+
public void AlterAddSortable()
828+
{
829+
IDatabase db = redisFixture.Redis.GetDatabase();
830+
db.Execute("FLUSHALL");
831+
var ft = db.FT();
832+
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);
833+
834+
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));
835+
836+
//sleep:
837+
System.Threading.Thread.Sleep(2000);
838+
839+
var fields = new HashEntry("title", "hello world");
840+
//fields.("title", "hello world");
841+
for (int i = 0; i < 100; i++)
842+
{
843+
db.HashSet($"doc{i}", fields.Name, fields.Value);
844+
}
845+
SearchResult res = ft.Search(index, new Query("hello world"));
846+
Assert.Equal(100, res.TotalResults);
847+
848+
Assert.True(ft.Alter(index, new Schema().AddTagField("tags").AddTextField("name", weight: 0.5)));
849+
for (int i = 0; i < 100; i++)
850+
{
851+
var fields2 = new HashEntry[] { new("name", "name" + i),
852+
new("tags", $"tagA,tagB,tag{i}") };
853+
// assertTrue(client.updateDocument(string.format("doc%d", i), 1.0, fields2));
854+
db.HashSet($"doc{i}", fields2);
855+
}
856+
SearchResult res2 = ft.Search(index, new Query("@tags:{tagA}"));
857+
Assert.Equal(100, res2.TotalResults);
858+
859+
var info = ft.Info(index);
860+
Assert.Equal(index, info.IndexName);
861+
Assert.Empty(info.IndexOption);
862+
// Assert.Equal(,info.IndexDefinition);
863+
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
864+
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
865+
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
866+
Assert.Equal(100, info.NumDocs);
867+
Assert.NotNull(info.MaxDocId);
868+
Assert.Equal(102, info.NumTerms);
869+
Assert.True(info.NumRecords >= 200);
870+
Assert.True(info.InvertedSzMebibytes < 1); // TODO: check this line and all the <1 lines
871+
Assert.Equal(0, info.VectorIndexSzMebibytes);
872+
Assert.Equal(208, info.TotalInvertedIndexBlocks);
873+
Assert.True(info.OffsetVectorsSzMebibytes < 1);
874+
Assert.True(info.DocTableSizeMebibytes < 1);
875+
Assert.Equal(0, info.SortableValueSizeMebibytes);
876+
Assert.True(info.KeyTableSizeMebibytes < 1);
877+
Assert.Equal(8, (int)info.RecordsPerDocAvg);
878+
Assert.True(info.BytesPerRecordAvg > 5);
879+
Assert.True(info.OffsetsPerTermAvg > 0.8);
880+
Assert.Equal(8, info.OffsetBitsPerRecordAvg);
881+
Assert.Equal(0, info.HashIndexingFailures);
882+
Assert.True(info.TotalIndexingTime > 0);
883+
Assert.Equal(0, info.Indexing);
884+
Assert.Equal(1, info.PercentIndexed);
885+
Assert.Equal(4, info.NumberOfUses);
886+
Assert.Equal(7, info.GcStats.Count);
887+
Assert.Equal(4, info.CursorStats.Count);
888+
}
889+
890+
[SkipIfRedis(Is.OSSCluster, Is.Enterprise)]
891+
public async Task AlterAddSortableAsync()
892+
{
893+
IDatabase db = redisFixture.Redis.GetDatabase();
894+
db.Execute("FLUSHALL");
895+
var ft = db.FT();
896+
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);
897+
898+
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));
899+
900+
//sleep:
901+
System.Threading.Thread.Sleep(2000);
902+
903+
var fields = new HashEntry("title", "hello world");
904+
//fields.("title", "hello world");
905+
for (int i = 0; i < 100; i++)
906+
{
907+
db.HashSet($"doc{i}", fields.Name, fields.Value);
908+
}
909+
SearchResult res = ft.Search(index, new Query("hello world"));
910+
Assert.Equal(100, res.TotalResults);
911+
912+
Assert.True(await ft.AlterAsync(index, new Schema().AddTagField("tags").AddTextField("name", weight: 0.5)));
913+
for (int i = 0; i < 100; i++)
914+
{
915+
var fields2 = new HashEntry[] { new("name", "name" + i),
916+
new("tags", $"tagA,tagB,tag{i}") };
917+
// assertTrue(client.updateDocument(string.format("doc%d", i), 1.0, fields2));
918+
db.HashSet($"doc{i}", fields2);
919+
}
920+
SearchResult res2 = ft.Search(index, new Query("@tags:{tagA}"));
921+
Assert.Equal(100, res2.TotalResults);
922+
923+
var info = await ft.InfoAsync(index);
924+
Assert.Equal(index, info.IndexName);
925+
Assert.Equal("title", info.Attributes[0]["identifier"].ToString());
926+
Assert.Equal("TAG", info.Attributes[1]["type"].ToString());
927+
Assert.Equal("name", info.Attributes[2]["attribute"].ToString());
801928
Assert.Equal(100, info.NumDocs);
802929
Assert.Equal("300", info.MaxDocId);
803930
Assert.Equal(102, info.NumTerms);

0 commit comments

Comments
 (0)