Skip to content

AzureDocumentDB: fix CosineDistance scores and score threshold, support CosineSimilarity - #78

Open
Laurianti wants to merge 3 commits into
CommunityToolkit:mainfrom
Laurianti:documentdb-cosine-distance
Open

Laurianti wants to merge 3 commits into
CommunityToolkit:mainfrom
Laurianti:documentdb-cosine-distance

Conversation

@Laurianti

Copy link
Copy Markdown
Member

Fixes #72.

Fix

  • CosineDistance, the default. COS returns the cosine similarity, and the provider returned it as the score, so the best match had the highest score. ScoreThreshold kept scores >= the threshold, so a threshold on the distance dropped the closest records. Now the score is 1 - similarity and ScoreThreshold keeps distances <= it, as InMemory does. In the repro of AzureDocumentDB: CosineDistance returns similarity scores, and ScoreThreshold filters the wrong way #72: scores 0, 1, 2, and ScoreThreshold = 1.5 keeps same and orthogonal.

Changes

  • CosineSimilarity maps to COS, with the score as it is and ScoreThreshold as a minimum, as on Chroma, InMemory, PgVector and Redis, which take both CosineSimilarity and CosineDistance. Before, it threw NotSupportedException.
  • Provider version bumped to 1.1.0.

Tests

  • 15 new unit test cases for the index similarity, the score and the threshold; 6 of them fail without the fix. SearchReturnsRecordWithScoreAsync now expects the distance, and fails without the fix too.
  • The distance function conformance tests now check the scores, and run CosineSimilarity. Without the fix, CosineDistance fails on the scores and CosineSimilarity throws.
  • Conformance tests on documentdb-local: 215 passed on net10.0 and 213 on net472.

Laurianti and others added 3 commits October 27, 2026 10:23
…hreshold keeps distances up to it

COS returns the cosine similarity. With CosineDistance, the default, the provider returned that similarity as the score and
kept scores >= ScoreThreshold. It now returns 1 - similarity and keeps distances <= ScoreThreshold, as InMemory does.
The distance function conformance tests now check the scores.
COS returns the cosine similarity, so CosineSimilarity maps to it with the score as it is and keeps scores >= ScoreThreshold.
Chroma, InMemory, PgVector and Redis already take both CosineSimilarity and CosineDistance.

Co-authored-by: Empiree <Empiree@users.noreply.github.com>
@adamsitnik
adamsitnik requested review from Empiree and a balanced review from Copilot October 27, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Static review found no blocking issues in the focused scoring changes or regression coverage; tests were not executed during review.

No open issues 🎉

What changed in this PR

Fixes #72 by aligning AzureDocumentDB’s cosine-distance scores and thresholds with other vector-store providers, while adding explicit cosine-similarity support.

Changes:

  • Converts cosine similarity to distance and translates distance thresholds into equivalent similarity filters.
  • Supports CosineSimilarity through the COS index metric and bumps the provider to 1.1.0.
  • Adds unit coverage and enables conformance checks for scores and cosine similarity.
File Description
MEVD/​test/​AzureDocumentDB.UnitTests/​DocumentDBCollectionTests.cs Updates the expected search score to cosine distance.
MEVD/​test/​AzureDocumentDB.UnitTests/​DocumentDBCollectionDistanceFunctionTests.cs Adds index, score, and threshold mapping tests.
MEVD/​test/​AzureDocumentDB.ConformanceTests/​DocumentDBDistanceFunctionTests.cs Enables score assertions and cosine-similarity tests.
MEVD/​src/​AzureDocumentDB/​DocumentDBCollectionSearchMapping.cs Converts scores and translates cosine-distance thresholds.
MEVD/​src/​AzureDocumentDB/​DocumentDBCollectionCreateMapping.cs Maps cosine similarity to COS.
MEVD/​src/​AzureDocumentDB/​DocumentDBCollection.cs Applies score conversion using the selected vector property.
MEVD/​src/​AzureDocumentDB/​AzureDocumentDB.csproj Updates the provider version to 1.1.0.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AzureDocumentDB: CosineDistance returns similarity scores, and ScoreThreshold filters the wrong way

2 participants