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

.Net: Fix collection create bug for redis hashsets. #8322

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
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public async Task<bool> CollectionExistsAsync(CancellationToken cancellationToke
public Task CreateCollectionAsync(CancellationToken cancellationToken = default)
{
// Map the record definition to a schema.
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(this._vectorStoreRecordDefinition.Properties, this._storagePropertyNames);
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(this._vectorStoreRecordDefinition.Properties, this._storagePropertyNames, useDollarPrefix: false);

// Create the index creation params.
// Add the collection name and colon as the index prefix, which means that any record where the key is prefixed with this text will be indexed by this index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task<bool> CollectionExistsAsync(CancellationToken cancellationToke
public Task CreateCollectionAsync(CancellationToken cancellationToken = default)
{
// Map the record definition to a schema.
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(this._vectorStoreRecordDefinition.Properties, this._storagePropertyNames);
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(this._vectorStoreRecordDefinition.Properties, this._storagePropertyNames, useDollarPrefix: true);

// Create the index creation params.
// Add the collection name and colon as the index prefix, which means that any record where the key is prefixed with this text will be indexed by this index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ internal static class RedisVectorStoreCollectionCreateMapping
/// </summary>
/// <param name="properties">The property definitions to map from.</param>
/// <param name="storagePropertyNames">A dictionary that maps from a property name to the storage name that should be used when serializing it to json for data and vector properties.</param>
/// <param name="useDollarPrefix">A value indicating whether to include $. prefix for field names as required in JSON mode.</param>
/// <returns>The mapped Redis <see cref="Schema"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if there are missing required or unsupported configuration options set.</exception>
public static Schema MapToSchema(IEnumerable<VectorStoreRecordProperty> properties, Dictionary<string, string> storagePropertyNames)
public static Schema MapToSchema(IEnumerable<VectorStoreRecordProperty> properties, Dictionary<string, string> storagePropertyNames, bool useDollarPrefix)
westey-m marked this conversation as resolved.
Show resolved Hide resolved
{
var schema = new Schema();
var fieldNamePrefix = useDollarPrefix ? "$." : string.Empty;

// Loop through all properties and create the index fields.
foreach (var property in properties)
Expand All @@ -79,7 +81,7 @@ public static Schema MapToSchema(IEnumerable<VectorStoreRecordProperty> properti
{
if (dataProperty.PropertyType == typeof(string) || (typeof(IEnumerable).IsAssignableFrom(dataProperty.PropertyType) && GetEnumerableType(dataProperty.PropertyType) == typeof(string)))
{
schema.AddTextField(new FieldName($"$.{storageName}", storageName));
schema.AddTextField(new FieldName($"{fieldNamePrefix}{storageName}", storageName));
}
else
{
Expand All @@ -92,15 +94,15 @@ public static Schema MapToSchema(IEnumerable<VectorStoreRecordProperty> properti
{
if (dataProperty.PropertyType == typeof(string))
{
schema.AddTagField(new FieldName($"$.{storageName}", storageName));
schema.AddTagField(new FieldName($"{fieldNamePrefix}{storageName}", storageName));
}
else if (typeof(IEnumerable).IsAssignableFrom(dataProperty.PropertyType) && GetEnumerableType(dataProperty.PropertyType) == typeof(string))
{
schema.AddTagField(new FieldName($"$.{storageName}.*", storageName));
schema.AddTagField(new FieldName($"{fieldNamePrefix}{storageName}.*", storageName));
}
else if (RedisVectorStoreCollectionCreateMapping.s_supportedFilterableNumericDataTypes.Contains(dataProperty.PropertyType))
{
schema.AddNumericField(new FieldName($"$.{storageName}", storageName));
schema.AddNumericField(new FieldName($"{fieldNamePrefix}{storageName}", storageName));
}
else
{
Expand All @@ -123,7 +125,7 @@ public static Schema MapToSchema(IEnumerable<VectorStoreRecordProperty> properti
var indexKind = GetSDKIndexKind(vectorProperty);
var distanceAlgorithm = GetSDKDistanceAlgorithm(vectorProperty);
var dimensions = vectorProperty.Dimensions.Value.ToString(CultureInfo.InvariantCulture);
schema.AddVectorField(new FieldName($"$.{storageName}", storageName), indexKind, new Dictionary<string, object>()
schema.AddVectorField(new FieldName($"{fieldNamePrefix}{storageName}", storageName), indexKind, new Dictionary<string, object>()
{
["TYPE"] = "FLOAT32",
["DIM"] = dimensions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public async Task CanCreateCollectionAsync()
1,
"testcollection:",
"SCHEMA",
"$.OriginalNameData",
"OriginalNameData",
"AS",
"OriginalNameData",
"TAG",
"$.data_storage_name",
"data_storage_name",
"AS",
"data_storage_name",
"TAG",
"$.vector_storage_name",
"vector_storage_name",
"AS",
"vector_storage_name",
"VECTOR",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace Microsoft.SemanticKernel.Connectors.Redis.UnitTests;
/// </summary>
public class RedisVectorStoreCollectionCreateMappingTests
{
[Fact]
public void MapToSchemaCreatesSchema()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void MapToSchemaCreatesSchema(bool useDollarPrefix)
{
// Arrange.
var properties = new VectorStoreRecordProperty[]
Expand Down Expand Up @@ -50,7 +52,7 @@ public void MapToSchemaCreatesSchema()
};

// Act.
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(properties, storagePropertyNames);
var schema = RedisVectorStoreCollectionCreateMapping.MapToSchema(properties, storagePropertyNames, useDollarPrefix);

// Assert.
Assert.NotNull(schema);
Expand All @@ -65,16 +67,32 @@ public void MapToSchemaCreatesSchema()
Assert.IsType<VectorField>(schema.Fields[6]);
Assert.IsType<VectorField>(schema.Fields[7]);

VerifyFieldName(schema.Fields[0].FieldName, new List<object> { "$.FilterableString", "AS", "FilterableString" });
VerifyFieldName(schema.Fields[1].FieldName, new List<object> { "$.FullTextSearchableString", "AS", "FullTextSearchableString" });
VerifyFieldName(schema.Fields[2].FieldName, new List<object> { "$.FilterableStringEnumerable.*", "AS", "FilterableStringEnumerable" });
VerifyFieldName(schema.Fields[3].FieldName, new List<object> { "$.FullTextSearchableStringEnumerable", "AS", "FullTextSearchableStringEnumerable" });
if (useDollarPrefix)
{
VerifyFieldName(schema.Fields[0].FieldName, new List<object> { "$.FilterableString", "AS", "FilterableString" });
VerifyFieldName(schema.Fields[1].FieldName, new List<object> { "$.FullTextSearchableString", "AS", "FullTextSearchableString" });
VerifyFieldName(schema.Fields[2].FieldName, new List<object> { "$.FilterableStringEnumerable.*", "AS", "FilterableStringEnumerable" });
VerifyFieldName(schema.Fields[3].FieldName, new List<object> { "$.FullTextSearchableStringEnumerable", "AS", "FullTextSearchableStringEnumerable" });

VerifyFieldName(schema.Fields[4].FieldName, new List<object> { "$.FilterableInt", "AS", "FilterableInt" });
VerifyFieldName(schema.Fields[5].FieldName, new List<object> { "$.FilterableNullableInt", "AS", "FilterableNullableInt" });

VerifyFieldName(schema.Fields[6].FieldName, new List<object> { "$.VectorDefaultIndexingOptions", "AS", "VectorDefaultIndexingOptions" });
VerifyFieldName(schema.Fields[7].FieldName, new List<object> { "$.vector_specific_indexing_options", "AS", "vector_specific_indexing_options" });
}
else
{
VerifyFieldName(schema.Fields[0].FieldName, new List<object> { "FilterableString", "AS", "FilterableString" });
VerifyFieldName(schema.Fields[1].FieldName, new List<object> { "FullTextSearchableString", "AS", "FullTextSearchableString" });
VerifyFieldName(schema.Fields[2].FieldName, new List<object> { "FilterableStringEnumerable.*", "AS", "FilterableStringEnumerable" });
VerifyFieldName(schema.Fields[3].FieldName, new List<object> { "FullTextSearchableStringEnumerable", "AS", "FullTextSearchableStringEnumerable" });

VerifyFieldName(schema.Fields[4].FieldName, new List<object> { "$.FilterableInt", "AS", "FilterableInt" });
VerifyFieldName(schema.Fields[5].FieldName, new List<object> { "$.FilterableNullableInt", "AS", "FilterableNullableInt" });
VerifyFieldName(schema.Fields[4].FieldName, new List<object> { "FilterableInt", "AS", "FilterableInt" });
VerifyFieldName(schema.Fields[5].FieldName, new List<object> { "FilterableNullableInt", "AS", "FilterableNullableInt" });

VerifyFieldName(schema.Fields[6].FieldName, new List<object> { "$.VectorDefaultIndexingOptions", "AS", "VectorDefaultIndexingOptions" });
VerifyFieldName(schema.Fields[7].FieldName, new List<object> { "$.vector_specific_indexing_options", "AS", "vector_specific_indexing_options" });
VerifyFieldName(schema.Fields[6].FieldName, new List<object> { "VectorDefaultIndexingOptions", "AS", "VectorDefaultIndexingOptions" });
VerifyFieldName(schema.Fields[7].FieldName, new List<object> { "vector_specific_indexing_options", "AS", "vector_specific_indexing_options" });
}

Assert.Equal("10", ((VectorField)schema.Fields[6]).Attributes!["DIM"]);
Assert.Equal("FLOAT32", ((VectorField)schema.Fields[6]).Attributes!["TYPE"]);
Expand All @@ -95,7 +113,7 @@ public void MapToSchemaThrowsOnInvalidVectorDimensions(int? dimensions)
var storagePropertyNames = new Dictionary<string, string>() { { "VectorProperty", "VectorProperty" } };

// Act and assert.
Assert.Throws<InvalidOperationException>(() => RedisVectorStoreCollectionCreateMapping.MapToSchema(properties, storagePropertyNames));
Assert.Throws<InvalidOperationException>(() => RedisVectorStoreCollectionCreateMapping.MapToSchema(properties, storagePropertyNames, true));
}

[Fact]
Expand Down
Loading