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
2 changes: 1 addition & 1 deletion MEVD/src/AzureDocumentDB/AzureDocumentDB.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<AssemblyName>CommunityToolkit.VectorData.AzureDocumentDB</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFrameworks>net10.0;net8.0;netstandard2.1;net472</TargetFrameworks>
Expand Down
2 changes: 1 addition & 1 deletion MEVD/src/AzureDocumentDB/DocumentDBCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ _ when vectorProperty.EmbeddingGenerationDispatcher is not null
// Add score threshold filter as a $match stage if specified
if (options.ScoreThreshold.HasValue)
{
pipeline.Add(DocumentDBCollectionSearchMapping.GetScoreThresholdMatchQuery(ScorePropertyName, options.ScoreThreshold.Value));
pipeline.Add(DocumentDBCollectionSearchMapping.GetScoreThresholdMatchQuery(ScorePropertyName, options.ScoreThreshold.Value, vectorProperty.DistanceFunction));
}

const string OperationName = "Aggregate";
Expand Down
20 changes: 15 additions & 5 deletions MEVD/src/AzureDocumentDB/DocumentDBCollectionSearchMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,27 @@ public static BsonDocument GetProjectionQuery(string scorePropertyName, string d

/// <summary>Returns a $match stage to filter results by score threshold.</summary>
/// <remarks>
/// Azure DocumentDB returns a similarity score where higher values mean more similar,
/// so we filter with $gte to keep results at or above the threshold.
/// For COS and IP Azure DocumentDB returns a similarity score where higher values mean more similar,
/// so we filter with $gte. For L2 it returns a distance where lower values mean more similar,
/// so we filter with $lte.
/// </remarks>
public static BsonDocument GetScoreThresholdMatchQuery(string scorePropertyName, double scoreThreshold)
=> new()
public static BsonDocument GetScoreThresholdMatchQuery(string scorePropertyName, double scoreThreshold, string? distanceFunction)
{
var comparisonOperator = GetVectorPropertyDistanceFunction(distanceFunction) switch
{
DistanceFunction.CosineDistance or DistanceFunction.DotProductSimilarity => "$gte",
DistanceFunction.EuclideanDistance => "$lte",
_ => throw new NotSupportedException($"Score threshold is not supported for distance function '{distanceFunction}'.")
};

return new()
{
{
"$match", new BsonDocument
{
{ scorePropertyName, new BsonDocument { { "$gte", scoreThreshold } } }
{ scorePropertyName, new BsonDocument { { comparisonOperator, scoreThreshold } } }
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using AzureDocumentDB.ConformanceTests.Support;
using Microsoft.Extensions.VectorData;
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.Support;
using Xunit;
Expand All @@ -18,11 +17,6 @@ public class DocumentDBDistanceFunctionTests(DocumentDBDistanceFunctionTests.Fix
public override Task HammingDistance() => Assert.ThrowsAsync<NotSupportedException>(base.HammingDistance);
public override Task ManhattanDistance() => Assert.ThrowsAsync<NotSupportedException>(base.ManhattanDistance);

// AzureDocumentDB EuclideanDistance doesn't correctly filter by score threshold (returns all results).
// See https://github.com/CommunityToolkit/AI/issues/6.
protected override Task TestScoreThreshold(VectorStoreCollection<int, DistanceFunctionTests<int>.SearchRecord> collection)
=> Task.CompletedTask;

public new class Fixture() : DistanceFunctionTests<int>.Fixture
{
public override TestStore TestStore => DocumentDBTestStore.Instance;
Expand Down
Loading