langchain4j/langchain4j-cassandra
Eunbin Son 6d1af7497a
fix: Use configured similarity metric in CassandraEmbeddingStore search instead of hardcoded COSINE (#5507)
## Issue
<!-- Update with the real issue number once the issue above is filed -->
Closes #5506

## Change

`CassandraEmbeddingStore` builds its backing `MetadataVectorTable` with
the configured `CassandraSimilarityMetric` (COSINE, DOT_PRODUCT, or
EUCLIDEAN), exposed via `Builder.metric(...)` and
`BuilderAstra.metric(...)`.

But both `findRelevant(...)` overloads (reached by `search(...)`)
hardcoded `.metric(CassandraSimilarityMetric.COSINE)` on the `AnnQuery`.
A store configured with EUCLIDEAN or DOT_PRODUCT was therefore queried
with the COSINE CQL similarity function, returning wrong top-K results
on a normal call path with valid input.

This PR replaces the hardcoded COSINE with
`.metric(embeddingTable.getSimilarityMetric())` in both overloads, so
search honors the configured metric. The COSINE default path is
unchanged (backward compatible).

Scope: this PR fixes only the metric passed to the query. The
EUCLIDEAN/DOT_PRODUCT relevance-score mapping
(`RelevanceScore.fromCosineSimilarity`) is a separate design question,
left to a follow-up.

Tests: added `CassandraEmbeddingStoreTest` (Mockito `ArgumentCaptor`, no
Docker/API key). It captures the `AnnQuery` passed to `similaritySearch`
and asserts the metric matches the configured one for both overloads,
plus a COSINE-default case. Verified fail-then-pass: the EUCLIDEAN
assertions fail against the pre-fix code and pass after the fix.

Note: `CassandraEmbeddingStore.java` predates the palantir-java-format
ratchet, so `spotless:apply` reformatted the whole touched file (import
ordering, line wrapping) as CI requires. Functional change is the two
`.metric(...)` lines.

## General checklist
- [X] There are no breaking changes (API, behaviour)
- [X] I have added unit and/or integration tests for my change
- [X] The tests cover both positive and negative cases
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the core
and main modules <!-- change is isolated to langchain4j-cassandra -->
- [ ] I have added/updated the documentation <!-- bug fix; docs added
after approval per guidelines -->
- [ ] I have added an example in the examples repo <!-- not a big
feature -->
- [ ] I have added/updated Spring Boot starter(s) <!-- not applicable
-->

<!-- Checklist for adding new maven module: not applicable -->
<!-- Checklist for adding/changing embedding store integration: no
schema/persistence change; behavior fix only -->
2026-07-31 15:50:44 +02:00
..
src fix: Use configured similarity metric in CassandraEmbeddingStore search instead of hardcoded COSINE (#5507) 2026-07-31 15:50:44 +02:00
pom.xml Update versions to 1.19.0-SNAPSHOT and 1.19.0-beta29-SNAPSHOT 2026-07-17 13:42:56 +00:00