Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,13 @@ public Document mapRow(ResultSet rs, int rowNum) throws SQLException {
Map<String, Object> metadata = toMap(rs.getString(3));
float distance = rs.getFloat(4);

metadata.put("distance", distance);

return new Document(id, content, metadata);
// @formatter:off
return Document.builder()
.id(id)
.text(content)
.metadata(metadata)
.score(1.0 - distance)
.build(); // @formatter:on
}

private Map<String, Object> toMap(String source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ static Stream<Arguments> provideFilters() {
);
}

private static boolean isSortedByDistance(List<Document> docs) {
private static boolean isSortedByScore(List<Document> docs) {

List<Float> distances = docs.stream().map(doc -> (Float) doc.getMetadata().get("distance")).toList();
List<Double> scores = docs.stream().map(Document::getScore).toList();

if (CollectionUtils.isEmpty(distances) || distances.size() == 1) {
if (CollectionUtils.isEmpty(scores) || scores.size() == 1) {
return true;
}

Iterator<Float> iter = distances.iterator();
Float current;
Float previous = iter.next();
Iterator<Double> iter = scores.iterator();
Double current;
Double previous = iter.next();
while (iter.hasNext()) {
current = iter.next();
if (previous > current) {
if (previous < current) {
return false;
}
previous = current;
Expand Down Expand Up @@ -166,7 +166,8 @@ public void addAndSearch(String distanceType) {
assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(this.documents.get(2).getId());
assertThat(resultDoc.getMetadata()).containsKeys("meta2", "distance");
assertThat(resultDoc.getMetadata()).containsKeys("meta2");
assertThat(resultDoc.getScore()).isBetween(0.0, 1.0);

// Remove all documents from the store
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
Expand Down Expand Up @@ -315,7 +316,8 @@ public void documentUpdate(String distanceType) {
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(document.getId());
assertThat(resultDoc.getText()).isEqualTo("Spring AI rocks!!");
assertThat(resultDoc.getMetadata()).containsKeys("meta1", "distance");
assertThat(resultDoc.getMetadata()).containsKeys("meta1");
assertThat(resultDoc.getScore()).isBetween(0.0, 1.0);

Document sameIdDocument = new Document(document.getId(),
"The World is Big and Salvation Lurks Around the Corner",
Expand All @@ -329,7 +331,8 @@ public void documentUpdate(String distanceType) {
resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(document.getId());
assertThat(resultDoc.getText()).isEqualTo("The World is Big and Salvation Lurks Around the Corner");
assertThat(resultDoc.getMetadata()).containsKeys("meta2", "distance");
assertThat(resultDoc.getMetadata()).containsKeys("meta2");
assertThat(resultDoc.getScore()).isBetween(0.0, 1.0);

dropTable(context);
});
Expand All @@ -350,19 +353,14 @@ public void searchWithThreshold(String distanceType) {

assertThat(fullResult).hasSize(3);

assertThat(isSortedByDistance(fullResult)).isTrue();
assertThat(isSortedByScore(fullResult)).isTrue();

List<Float> distances = fullResult.stream()
.map(doc -> (Float) doc.getMetadata().get("distance"))
.toList();
List<Double> scores = fullResult.stream().map(Document::getScore).toList();

float threshold = (distances.get(0) + distances.get(1)) / 2;
double threshold = (scores.get(0) + scores.get(1)) / 2;

List<Document> results = vectorStore.similaritySearch(SearchRequest.builder()
.query("Time Shelter")
.topK(5)
.similarityThreshold(1 - threshold)
.build());
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Time Shelter").topK(5).similarityThreshold(threshold).build());

assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
Expand Down
Loading