Skip to content

Fix PgVectorStore overwriting a document's own distance metadata - #6983

Open
ss-bhatt wants to merge 1 commit into
spring-projects:mainfrom
ss-bhatt:fix/pgvector-distance-metadata-collision
Open

ss-bhatt wants to merge 1 commit into
spring-projects:mainfrom
ss-bhatt:fix/pgvector-distance-metadata-collision

Conversation

@ss-bhatt

Copy link
Copy Markdown

DocumentRowMapper.mapRow() always wrote the computed similarity distance into the metadata
map under the key distance, overwriting any document that already used that key for its own
data. Document already exposes the same value via getScore(), so the write was redundant.

Skip the write and log a warning when the key is already present, so existing metadata survives.

Testing

Added rowMapperSkipsDistanceMetadataWhenUserAlreadyUsesThatKey, which fails without the fix
(returns the computed distance instead of the caller's value) and passes with it, plus
rowMapperAddsDistanceMetadataWhenAbsent covering the normal case.

./mvnw -pl vector-stores/spring-ai-pgvector-store test passes (28 tests). Spring Java Format
and Checkstyle ran as part of that build with no violations.

DocumentRowMapper.mapRow() always wrote the computed similarity
distance into the metadata map under the key `distance`, overwriting
any document that already used that key. The same value is already
available via `Document#getScore()`, making the write redundant.

Skip the write and log a warning when the key is already present, so
existing metadata survives.

Signed-off-by: Srinadh Bhattiprolu <ss.bhattiprolu@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants