|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.io.StringReader;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.Map; |
22 | 23 | import java.util.Objects;
|
23 | 24 | import java.util.Optional;
|
24 | 25 | import java.util.stream.Collectors;
|
|
51 | 52 | import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
|
52 | 53 | import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
|
53 | 54 | import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
|
54 |
| -import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext.Builder; |
55 | 55 | import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
|
56 | 56 | import org.springframework.beans.factory.InitializingBean;
|
57 | 57 | import org.springframework.util.Assert;
|
@@ -144,11 +144,14 @@ public OpenSearchVectorStore withSimilarityFunction(String similarityFunction) {
|
144 | 144 |
|
145 | 145 | @Override
|
146 | 146 | 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); |
148 | 149 | BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder();
|
149 | 150 | 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))); |
152 | 155 | }
|
153 | 156 | bulkRequest(bulkRequestBuilder.build());
|
154 | 157 | }
|
@@ -292,4 +295,15 @@ else if ("l2".equalsIgnoreCase(this.similarityFunction)) {
|
292 | 295 | return this.similarityFunction;
|
293 | 296 | }
|
294 | 297 |
|
| 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 | + |
295 | 309 | }
|
0 commit comments