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
4 changes: 4 additions & 0 deletions src/DataStax.AstraDB.DataApi/Core/Query/FindEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public FindEnumerator<T, TResult, TSort> Sort(TSort sortBuilder)
/// var similarity = result.Similarity;
/// </code>
/// </example>
/// <remarks>
/// When searching on Tables, the field in the row class should be given the
/// attribute <see cref="SerDes.ColumnMappingAttribute"/> instead.
/// </remarks>
public FindEnumerator<T, TResult, TSort> IncludeSimilarity(bool includeSimilarity)
{
return UpdateOptions(options => options.IncludeSimilarity = includeSimilarity);
Expand Down
40 changes: 40 additions & 0 deletions src/DataStax.AstraDB.DataApi/SerDes/ColumnMappingAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace DataStax.AstraDB.DataApi.SerDes;

using System;

/// <summary>
/// Marks a property on a table row as a special field for the database's use.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ColumnMappingAttribute : Attribute
{
/// <summary>
/// The type of special field this property is mapped to
/// </summary>
public ColumnMappingField Field { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ColumnMappingAttribute"/> class.
/// </summary>
/// <param name="field">The type of special field this property is mapped to</param>
public ColumnMappingAttribute(ColumnMappingField field)
{
Field = field;
}
}
26 changes: 26 additions & 0 deletions src/DataStax.AstraDB.DataApi/SerDes/ColumnMappingField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace DataStax.AstraDB.DataApi.SerDes;

/// <summary>
/// Special field types for table rows.
/// </summary>
public enum ColumnMappingField
{
/// <summary>On read operations only, deserializes the similarity result for vector comparisons</summary>
Similarity,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace DataStax.AstraDB.DataApi.SerDes;
using System;

/// <summary>
/// Marks a property on a document as a special field for the database's use.
/// Marks a property on a collection document as a special field for the database's use.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class DocumentMappingAttribute : Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace DataStax.AstraDB.DataApi.SerDes;

/// <summary>
/// Special field types
/// Special field types for collection documents.
/// </summary>
public enum DocumentMappingField
{
Expand Down
10 changes: 6 additions & 4 deletions src/DataStax.AstraDB.DataApi/SerDes/RowConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial

T instance = Activator.CreateInstance<T>();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanWrite && !p.GetCustomAttributes<ColumnIgnoreAttribute>().Any())
.Where(p => p.CanWrite &&
(!p.GetCustomAttributes<ColumnIgnoreAttribute>().Any() ||
p.GetCustomAttribute<ColumnMappingAttribute>()?.Field == ColumnMappingField.Similarity))
.ToDictionary(p => GetPropertyName(p, true), p => p);

while (reader.Read())
Expand Down Expand Up @@ -228,10 +230,10 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions

private static string GetPropertyName(PropertyInfo property, bool forDeserialization)
{
var documentMappingAttribute = property.GetCustomAttribute<DocumentMappingAttribute>();
if (documentMappingAttribute != null && forDeserialization)
var columnMappingAttribute = property.GetCustomAttribute<ColumnMappingAttribute>();
if (columnMappingAttribute != null && forDeserialization)
{
if (documentMappingAttribute.Field == DocumentMappingField.Similarity)
if (columnMappingAttribute.Field == ColumnMappingField.Similarity)
{
return "$similarity";
}
Expand Down
4 changes: 2 additions & 2 deletions test/DataStax.AstraDB.DataApi.IntegrationTests/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SimpleObjectWithVector

public class SimpleObjectWithVectorSearchResult : SimpleObjectWithVector
{
[DocumentMapping(DocumentMappingField.Similarity)]
[ColumnMapping(ColumnMappingField.Similarity)]
public double? Similarity { get; set; }
}

Expand Down Expand Up @@ -225,7 +225,7 @@ public class RowEventByDay

public class RowBookWithSimilarity : RowBook
{
[DocumentMapping(DocumentMappingField.Similarity)]
[ColumnMapping(ColumnMappingField.Similarity)]
public double Similarity { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ public async Task FindOne_Vector()
var result = await table.FindOneAsync<SimpleObjectWithVectorSearchResult>(null,
new TableFindOptions<SimpleObjectWithVector>() { Sort = sort, IncludeSimilarity = true });

Assert.NotEqual(0, result.Similarity);
Assert.NotNull(result.Similarity);
Assert.True(result.Similarity > 0);
}
finally
{
Expand Down
Loading