Skip to content

Commit 2ef4559

Browse files
committed
Fix OpenSearch vector store's doAdd(List<Document> documents)
- Since Document's reference to its embedding is deprecated, store the embedding into OpenSearch vector store by creating an explicit OpenSearch Document type which has embedding associated with it
1 parent 3e9256f commit 2ef4559

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.StringReader;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Objects;
2324
import java.util.Optional;
2425
import java.util.stream.Collectors;
@@ -51,7 +52,6 @@
5152
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
5253
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
5354
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
54-
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext.Builder;
5555
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
5656
import org.springframework.beans.factory.InitializingBean;
5757
import org.springframework.util.Assert;
@@ -144,11 +144,14 @@ public OpenSearchVectorStore withSimilarityFunction(String similarityFunction) {
144144

145145
@Override
146146
public void doAdd(List<Document> documents) {
147-
this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(), this.batchingStrategy);
147+
List<float[]> embedding = this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(),
148+
this.batchingStrategy);
148149
BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder();
149150
for (Document document : documents) {
150-
bulkRequestBuilder
151-
.operations(op -> op.index(idx -> idx.index(this.index).id(document.getId()).document(document)));
151+
OpenSearchDocument openSearchDocument = new OpenSearchDocument(document.getId(), document.getContent(),
152+
document.getMetadata(), embedding.get(documents.indexOf(document)));
153+
bulkRequestBuilder.operations(op -> op
154+
.index(idx -> idx.index(this.index).id(openSearchDocument.id()).document(openSearchDocument)));
152155
}
153156
bulkRequest(bulkRequestBuilder.build());
154157
}
@@ -292,4 +295,15 @@ else if ("l2".equalsIgnoreCase(this.similarityFunction)) {
292295
return this.similarityFunction;
293296
}
294297

298+
/**
299+
* The representation of {@link Document} along with its embedding.
300+
*
301+
* @param id The id of the document
302+
* @param content The content of the document
303+
* @param metadata The metadata of the document
304+
* @param embedding The vectors representing the content of the document
305+
*/
306+
public record OpenSearchDocument(String id, String content, Map<String, Object> metadata, float[] embedding) {
307+
}
308+
295309
}

0 commit comments

Comments
 (0)