Repository navigation
AzureDocumentDB: fix CosineDistance scores and score threshold, support CosineSimilarity - #78
Merged
adamsitnik merged 3 commits intoOct 28, 2026
Conversation
…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>
Contributor
There was a problem hiding this comment.
🟢 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
CosineSimilaritythrough 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
adamsitnik
approved these changes
Oct 28, 2026
adamsitnik
left a comment
Contributor
There was a problem hiding this comment.
The changes look great, thank you @Laurianti !
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.ScoreThresholdkept scores>=the threshold, so a threshold on the distance dropped the closest records. Now the score is1 - similarityandScoreThresholdkeeps 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, andScoreThreshold = 1.5keepssameandorthogonal.Changes
CosineSimilaritymaps to COS, with the score as it is andScoreThresholdas a minimum, as on Chroma, InMemory, PgVector and Redis, which take bothCosineSimilarityandCosineDistance. Before, it threwNotSupportedException.Tests
SearchReturnsRecordWithScoreAsyncnow expects the distance, and fails without the fix too.CosineSimilarity. Without the fix,CosineDistancefails on the scores andCosineSimilaritythrows.documentdb-local: 215 passed on net10.0 and 213 on net472.