Skip to content

Commit 3a740ec

Browse files
committed
Handle PR comments
Signed-off-by: Thomas Vitale <[email protected]>
1 parent d262c4c commit 3a740ec

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

spring-ai-core/src/main/java/org/springframework/ai/document/Document.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class Document implements MediaContent {
7171
* measure.
7272
*/
7373
@Nullable
74-
private Double score;
74+
private final Double score;
7575

7676
/**
7777
* Embedding of the document. Note: ephemeral field.
@@ -90,10 +90,6 @@ public Document(@JsonProperty("content") String content) {
9090
this(content, new HashMap<>());
9191
}
9292

93-
/**
94-
* @deprecated Use builder instead: {@link Document#builder()}.
95-
*/
96-
@Deprecated(since = "1.0.0-M5", forRemoval = true)
9793
public Document(String content, Map<String, Object> metadata) {
9894
this(content, metadata, new RandomIdGenerator());
9995
}
@@ -114,10 +110,6 @@ public Document(String content, Map<String, Object> metadata, IdGenerator idGene
114110
this(idGenerator.generateId(content, metadata), content, metadata);
115111
}
116112

117-
/**
118-
* @deprecated Use builder instead: {@link Document#builder()}.
119-
*/
120-
@Deprecated(since = "1.0.0-M5", forRemoval = true)
121113
public Document(String id, String content, Map<String, Object> metadata) {
122114
this(id, content, List.of(), metadata);
123115
}
@@ -194,10 +186,6 @@ public Double getScore() {
194186
return this.score;
195187
}
196188

197-
public void setScore(@Nullable Double score) {
198-
this.score = score;
199-
}
200-
201189
/**
202190
* Return the embedding that were calculated.
203191
* @deprecated We are considering getting rid of this, please comment on
@@ -233,20 +221,27 @@ public void setContentFormatter(ContentFormatter contentFormatter) {
233221
this.contentFormatter = contentFormatter;
234222
}
235223

236-
@Override
237-
public int hashCode() {
238-
return Objects.hash(id, content, media, metadata);
224+
public Builder mutate() {
225+
return new Builder().id(this.id)
226+
.content(this.content)
227+
.media(new ArrayList<>(this.media))
228+
.metadata(this.metadata)
229+
.score(this.score);
239230
}
240231

241232
@Override
242233
public boolean equals(Object o) {
243-
if (this == o)
244-
return true;
245234
if (o == null || getClass() != o.getClass())
246235
return false;
247236
Document document = (Document) o;
248237
return Objects.equals(id, document.id) && Objects.equals(content, document.content)
249-
&& Objects.equals(media, document.media) && Objects.equals(metadata, document.metadata);
238+
&& Objects.equals(media, document.media) && Objects.equals(metadata, document.metadata)
239+
&& Objects.equals(score, document.score);
240+
}
241+
242+
@Override
243+
public int hashCode() {
244+
return Objects.hash(id, content, media, metadata, score);
250245
}
251246

252247
@Override
@@ -267,6 +262,7 @@ public static class Builder {
267262

268263
private float[] embedding = new float[0];
269264

265+
@Nullable
270266
private Double score;
271267

272268
private IdGenerator idGenerator = new RandomIdGenerator();
@@ -314,7 +310,7 @@ public Builder embedding(float[] embedding) {
314310
return this;
315311
}
316312

317-
public Builder score(Double score) {
313+
public Builder score(@Nullable Double score) {
318314
this.score = score;
319315
return this;
320316
}

vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/ElasticsearchVectorStore.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,12 @@ private String getElasticsearchQueryString(Filter.Expression filterExpression) {
211211

212212
private Document toDocument(Hit<Document> hit) {
213213
Document document = hit.source();
214+
Document.Builder documentBuilder = document.mutate();
214215
if (hit.score() != null) {
215-
document.getMetadata().put(DocumentMetadata.DISTANCE.value(), 1 - normalizeSimilarityScore(hit.score()));
216-
document.setScore(normalizeSimilarityScore(hit.score()));
216+
documentBuilder.metadata(DocumentMetadata.DISTANCE.value(), 1 - normalizeSimilarityScore(hit.score()));
217+
documentBuilder.score(normalizeSimilarityScore(hit.score()));
217218
}
218-
return document;
219+
return documentBuilder.build();
219220
}
220221

221222
// more info on score/distance calculation

vector-stores/spring-ai-opensearch-store/src/main/java/org/springframework/ai/vectorstore/OpenSearchVectorStore.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,12 @@ private List<Document> similaritySearch(org.opensearch.client.opensearch.core.Se
232232

233233
private Document toDocument(Hit<Document> hit) {
234234
Document document = hit.source();
235+
Document.Builder documentBuilder = document.mutate();
235236
if (hit.score() != null) {
236-
document.setScore(hit.score());
237-
document.getMetadata().put(DocumentMetadata.DISTANCE.value(), 1 - hit.score().floatValue());
237+
documentBuilder.metadata(DocumentMetadata.DISTANCE.value(), 1 - hit.score().floatValue());
238+
documentBuilder.score(hit.score());
238239
}
239-
return document;
240+
return documentBuilder.build();
240241
}
241242

242243
public boolean exists(String targetIndex) {

0 commit comments

Comments
 (0)