diff --git a/src/NRedisStack/Search/DataTypes/InfoResult.cs b/src/NRedisStack/Search/DataTypes/InfoResult.cs index ac39a3a8..b676ca24 100644 --- a/src/NRedisStack/Search/DataTypes/InfoResult.cs +++ b/src/NRedisStack/Search/DataTypes/InfoResult.cs @@ -5,6 +5,7 @@ namespace NRedisStack.Search.DataTypes; public class InfoResult { private readonly Dictionary _all = new(); + private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE" }; public string IndexName => GetString("index_name")!; public Dictionary IndexOption => GetRedisResultDictionary("index_options")!; public Dictionary[] Attributes => GetRedisResultDictionaryArray("attributes")!; @@ -91,7 +92,6 @@ private double GetDouble(string key) } return result; - } private Dictionary[]? GetRedisResultDictionaryArray(string key) @@ -105,12 +105,17 @@ private double GetDouble(string key) var dict = new Dictionary(); 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; } } \ No newline at end of file diff --git a/tests/NRedisStack.Tests/Examples/ExampleTests.cs b/tests/NRedisStack.Tests/Examples/ExampleTests.cs index 40e9fd21..fc257b98 100644 --- a/tests/NRedisStack.Tests/Examples/ExampleTests.cs +++ b/tests/NRedisStack.Tests/Examples/ExampleTests.cs @@ -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:"), @@ -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() @@ -1442,4 +1442,4 @@ private static void SortAndCompare(List expectedList, List res) Assert.Equal(expectedList[i], res[i].ToString()); } } -} \ No newline at end of file +} diff --git a/tests/NRedisStack.Tests/Search/SearchTests.cs b/tests/NRedisStack.Tests/Search/SearchTests.cs index e644a7af..9838753c 100644 --- a/tests/NRedisStack.Tests/Search/SearchTests.cs +++ b/tests/NRedisStack.Tests/Search/SearchTests.cs @@ -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)); @@ -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); @@ -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);