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

Handle INDEXEMPTY and INDEXMISSING in FT.INFO response #356

Merged
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
39 changes: 25 additions & 14 deletions src/NRedisStack/Search/DataTypes/InfoResult.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using StackExchange.Redis;
using System.Reflection.Emit;
using StackExchange.Redis;

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" };
private Dictionary<string, RedisResult>[] _attributes;
private Dictionary<string, RedisResult> _indexOption;
private Dictionary<string, RedisResult> _gcStats;
private Dictionary<string, RedisResult> _cursorStats;

private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE", "INDEXEMPTY", "INDEXMISSING" };
public string IndexName => GetString("index_name")!;
public Dictionary<string, RedisResult> IndexOption => GetRedisResultDictionary("index_options")!;
public Dictionary<string, RedisResult>[] Attributes => GetRedisResultDictionaryArray("attributes")!;
public Dictionary<string, RedisResult> IndexOption => _indexOption = _indexOption ?? GetRedisResultDictionary("index_options")!;
public Dictionary<string, RedisResult>[] Attributes => _attributes = _attributes ?? GetAttributesAsDictionaryArray()!;
public long NumDocs => GetLong("num_docs");
public string MaxDocId => GetString("max_doc_id")!;
public long NumTerms => GetLong("num_terms");
Expand Down Expand Up @@ -48,9 +54,9 @@ public class InfoResult
public long NumberOfUses => GetLong("number_of_uses");


public Dictionary<string, RedisResult> GcStats => GetRedisResultDictionary("gc_stats")!;
public Dictionary<string, RedisResult> GcStats => _gcStats = _gcStats ?? GetRedisResultDictionary("gc_stats")!;

public Dictionary<string, RedisResult> CursorStats => GetRedisResultDictionary("cursor_stats")!;
public Dictionary<string, RedisResult> CursorStats => _cursorStats = _cursorStats ?? GetRedisResultDictionary("cursor_stats")!;

public InfoResult(RedisResult result)
{
Expand Down Expand Up @@ -94,24 +100,29 @@ private double GetDouble(string key)
return result;
}

private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key)
private Dictionary<string, RedisResult>[]? GetAttributesAsDictionaryArray()
{
if (!_all.TryGetValue(key, out var value)) return default;
if (!_all.TryGetValue("attributes", out var value)) return default;
var values = (RedisResult[])value!;
var result = new Dictionary<string, RedisResult>[values.Length];
for (int i = 0; i < values.Length; i++)
{
var fv = (RedisResult[])values[i]!;
var dict = new Dictionary<string, RedisResult>();
for (int j = 0; j < fv.Length; j += 2)

IEnumerable<RedisResult> enumerable = (RedisResult[])values[i]!;
IEnumerator<RedisResult> results = enumerable.GetEnumerator();
while (results.MoveNext())
{
if (booleanAttributes.Contains((string)fv[j]!))
string attribute = (string)results.Current;
// if its boolean attributes add itself to the dictionary and continue
if (booleanAttributes.Contains(attribute))
{
dict.Add((string)fv[j]!, fv[j--]);
dict.Add(attribute, results.Current);
}
else
{
dict.Add((string)fv[j]!, fv[j + 1]);
{//if its not a boolean attribute, add the next item as value to the dictionary
results.MoveNext(); ;
dict.Add(attribute, results.Current);
}
}
result[i] = dict;
Expand Down
34 changes: 34 additions & 0 deletions tests/NRedisStack.Tests/Search/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,40 @@ public void AlterAddSortable()
Assert.Equal(4, info.CursorStats.Count);
}

[SkipIfRedis(Comparison.LessThan, "7.3.0")]
public void InfoWithIndexEmptyAndIndexMissing()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT(2);
var vectorAttrs = new Dictionary<string, object>()
{
["TYPE"] = "FLOAT32",
["DIM"] = "2",
["DISTANCE_METRIC"] = "L2",
};

Schema sc = new Schema()
.AddTextField("text1", 1.0, emptyIndex: true, missingIndex: true)
.AddTagField("tag1", emptyIndex: true, missingIndex: true)
.AddNumericField("numeric1", missingIndex: true)
.AddGeoField("geo1", missingIndex: true)
.AddGeoShapeField("geoshape1", Schema.GeoShapeField.CoordinateSystem.FLAT, missingIndex: true)
.AddVectorField("vector1", Schema.VectorField.VectorAlgo.FLAT, vectorAttrs, missingIndex: true);
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

var info = ft.Info(index);
var attributes = info.Attributes;
foreach (var attribute in attributes)
{
Assert.True(attribute.ContainsKey("INDEXMISSING"));
if (attribute["attribute"].ToString() == "text1" || attribute["attribute"].ToString() == "tag1")
{
Assert.True(attribute.ContainsKey("INDEXEMPTY"));
}
}
}

[SkipIfRedis(Is.OSSCluster, Is.Enterprise)]
public async Task AlterAddSortableAsync()
{
Expand Down
Loading