Skip to content

fix(rag): normalize Milvus L2 retrieval scores - #3070

Open
liugy789 wants to merge 1 commit into
agentscope-ai:mainfrom
liugy789:fix/milvus-l2-score-normalization
Open

fix(rag): normalize Milvus L2 retrieval scores#3070
liugy789 wants to merge 1 commit into
agentscope-ai:mainfrom
liugy789:fix/milvus-l2-score-normalization

Conversation

@liugy789

@liugy789 liugy789 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Pass the configured metric type to the Milvus SearchReq.
  • Normalize L2 distances into higher-is-better scores.
  • Apply scoreThreshold after score normalization.
  • Add regression tests for score conversion, metric propagation, and thresholding.

Why

Milvus L2 search returns distances where smaller values indicate greater similarity. AgentScope retrieval uses a higher-is-better score contract, so raw L2 distances must be adapted before they are exposed as document scores.

This PR uses 1 / (1 + distance) as an AgentScope-side normalization formula. This formula is not a Milvus-defined conversion.

Validation

  • MilvusStoreTest: 65 tests passed.
  • RAG simple module: 449 tests passed.
  • Spotless check passed.
  • git diff --check passed.

References

Fixes #3069

@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dailingtao dailingtao 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.

LGTM

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

Right problem, right direction: raw Milvus L2 distances were fed straight into the higher-is-better score contract and then compared against scoreThreshold, so L2 collections filtered essentially backwards. Passing the configured metric to SearchReq and normalizing before thresholding fixes it, with tests for conversion, propagation and ordering. Two compatibility asks: validate configured metric against the existing collection index, and call out the threshold retuning in a changelog.


Automated review by github-manager-bot

.databaseName(databaseName)
.collectionName(collectionName)
.data(Collections.singletonList(queryVector))
.metricType(metricType)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Passing metricType explicitly to SearchReq is new behaviour: previously Milvus searched with the metric recorded on the collection index, now it searches with the configured metric. When a store connects to an existing collection whose index metric differs from the builder default (COSINE), this turns a previously-working search into a provider-side error or wrong ranking. Could you validate the configured metric against describeIndex/describeCollection at connect time and fail fast with a message naming both values? The javadoc note alone will be easy to miss for people who configured the store before this change.

* scores in the expected direction and are left unchanged.
*
* @param rawScore the raw score or distance returned by Milvus
* @return a higher-is-better score

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

1 / (1 + distance) is a reasonable monotone map into (0, 1], and applying the threshold after normalization is the correct order. This is still a breaking change for anyone who already tuned scoreThreshold against raw L2 distances, and the resulting scores are not comparable with COSINE/IP scores. Worth a changelog entry plus a note in the RAG docs that thresholds must be re-tuned for L2 collections.

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.

[Bug] MilvusStore returns inverted scores for the L2 metric

3 participants