Skip to content
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
6 changes: 3 additions & 3 deletions src/Redis.OM/Modeling/Vectors/VectorJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public override void Write(Utf8JsonWriter writer, Vector<T> value, JsonSerialize
return;
}

if (_vectorizerAttribute is FloatVectorizerAttribute && value is Vector<double[]> floatVector)
if (_vectorizerAttribute is FloatVectorizerAttribute && value is Vector<float[]> floatVector)
{
writer.WriteStartArray();
foreach (var d in floatVector.Value)
foreach (var f in floatVector.Value)
{
writer.WriteNumberValue(d);
writer.WriteNumberValue(f);
}

writer.WriteEndArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ public class ToyVector
{
[RedisIdField] public string Id { get; set; }
[Indexed][DoubleVectorizer(6)]public Vector<double[]> SimpleVector { get; set; }
}

[Document(StorageType = StorageType.Json, Prefixes = new []{"FloatVec"})]
public class ObjectWithFloatVector
{
[RedisIdField] public string Id { get; set; }
[Indexed] public string Name { get; set; }
[Indexed(DistanceMetric = DistanceMetric.L2)][FloatVectorizer(2)] public Vector<float[]> Vec { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,42 @@ public void TestAzureOpenAIVectorizer()
Assert.Equal(obj.Sentence.Value, res.Sentence.Value);
Assert.Equal(obj.Sentence.Embedding, res.Sentence.Embedding);
}

[Fact]
public void FloatVectorIsIndexedAndEnumerable()
{
// Regression for #566: a Vector<float[]> + [FloatVectorizer] was serialized as an object
// ({"Value":...,"Vector":[...]}) instead of a flat array because the converter's type guard
// checked Vector<double[]>. The index points at the bare path expecting a vector, so the
// document failed to index (hash_indexing_failures) and every search - including the plain
// enumeration IRedisCollection<T> issues - came back empty even though the data was in Redis.
_connection.DropIndexAndAssociatedRecords(typeof(ObjectWithFloatVector));
_connection.CreateIndex(typeof(ObjectWithFloatVector));
try
{
var collection = new RedisCollection<ObjectWithFloatVector>(_connection);
var id = $"floatvec-{Guid.NewGuid():N}";
var embedding = new[] { 0.1f, 0.2f };
collection.Insert(new ObjectWithFloatVector
{
Id = id,
Name = "Hal",
Vec = Vector.Of(embedding),
});

// The document indexed cleanly - no failure swallowed at write time.
var info = _connection.GetIndexInfo(typeof(ObjectWithFloatVector));
Assert.Equal("0", info!.HashIndexingFailures);

// A plain enumeration (no predicate) returns the document, which was the reported symptom.
var all = collection.ToList();
var match = Assert.Single(all);
Assert.Equal(id, match.Id);
Assert.Equal(embedding, match.Vec.Value);
}
finally
{
_connection.DropIndexAndAssociatedRecords(typeof(ObjectWithFloatVector));
}
}
}
Loading