fix: align vectorstore live retrieval
This commit is contained in:
@@ -106,7 +106,7 @@ public class VectorSearchService {
|
||||
result.setMetadata(toJson(document.getMetadata()));
|
||||
result.setRawScore(document.getScore());
|
||||
result.setScoreLabel("similarity");
|
||||
result.setScore(toCompatibleL2Distance(document.getScore()));
|
||||
result.setScore(toCompatibleL2Distance(document));
|
||||
results.add(result);
|
||||
}
|
||||
logger.info("Spring AI VectorStore search complete, candidates={}", results.size());
|
||||
@@ -166,6 +166,14 @@ public class VectorSearchService {
|
||||
}
|
||||
}
|
||||
|
||||
private float toCompatibleL2Distance(Document document) {
|
||||
Double distance = extractDistance(document.getMetadata());
|
||||
if (distance != null) {
|
||||
return distance.floatValue();
|
||||
}
|
||||
return toCompatibleL2Distance(document.getScore());
|
||||
}
|
||||
|
||||
private float toCompatibleL2Distance(Double similarity) {
|
||||
if (similarity == null) {
|
||||
return (float) maxL2Distance;
|
||||
@@ -174,6 +182,24 @@ public class VectorSearchService {
|
||||
return (float) ((1.0 - bounded) * maxL2Distance);
|
||||
}
|
||||
|
||||
private Double extractDistance(Map<String, Object> metadata) {
|
||||
if (metadata == null) {
|
||||
return null;
|
||||
}
|
||||
Object value = metadata.get("distance");
|
||||
if (value instanceof Number number) {
|
||||
return number.doubleValue();
|
||||
}
|
||||
if (value instanceof String text) {
|
||||
try {
|
||||
return Double.parseDouble(text);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String toJson(Map<String, Object> metadata) {
|
||||
if (metadata == null || metadata.isEmpty()) {
|
||||
return null;
|
||||
@@ -196,8 +222,8 @@ public class VectorSearchService {
|
||||
private String content;
|
||||
/**
|
||||
* Compatibility score used by existing lookup relevance normalization.
|
||||
* SDK mode keeps L2 distance; VectorStore mode maps similarity into a
|
||||
* L2-like distance using retrieval.normalization.max-l2-distance.
|
||||
* SDK mode keeps L2 distance; VectorStore mode prefers the Milvus
|
||||
* distance metadata and falls back to similarity mapping.
|
||||
*/
|
||||
private float score;
|
||||
private Double rawScore;
|
||||
|
||||
Reference in New Issue
Block a user