|
21 | 21 | import java.util.Map; |
22 | 22 | import java.util.Optional; |
23 | 23 |
|
24 | | -import com.mongodb.BasicDBObject; |
25 | | - |
26 | 24 | import com.mongodb.MongoCommandException; |
27 | 25 | import org.springframework.ai.document.Document; |
28 | 26 | import org.springframework.ai.embedding.EmbeddingModel; |
@@ -140,16 +138,15 @@ private org.bson.Document createSearchIndexDefinition() { |
140 | 138 | } |
141 | 139 |
|
142 | 140 | /** |
143 | | - * Maps a BasicDBObject to a Spring AI Document |
144 | | - * @param basicDBObject the basicDBObject to map to a spring ai document |
145 | | - * @return the spring ai document |
| 141 | + * Maps a Bson Document to a Spring AI Document |
| 142 | + * @param mongoDocument the mongoDocument to map to a Spring AI Document |
| 143 | + * @return the Spring AI Document |
146 | 144 | */ |
147 | | - @SuppressWarnings("unchecked") |
148 | | - private Document mapBasicDbObject(BasicDBObject basicDBObject) { |
149 | | - String id = basicDBObject.getString(ID_FIELD_NAME); |
150 | | - String content = basicDBObject.getString(CONTENT_FIELD_NAME); |
151 | | - Map<String, Object> metadata = (Map<String, Object>) basicDBObject.get(METADATA_FIELD_NAME); |
152 | | - List<Double> embedding = (List<Double>) basicDBObject.get(this.config.pathName); |
| 145 | + private Document mapMongoDocument(org.bson.Document mongoDocument) { |
| 146 | + String id = mongoDocument.getString(ID_FIELD_NAME); |
| 147 | + String content = mongoDocument.getString(CONTENT_FIELD_NAME); |
| 148 | + Map<String, Object> metadata = mongoDocument.get(METADATA_FIELD_NAME, org.bson.Document.class); |
| 149 | + List<Double> embedding = mongoDocument.getList(this.config.pathName, Double.class); |
153 | 150 |
|
154 | 151 | Document document = new Document(id, content, metadata); |
155 | 152 | document.setEmbedding(embedding); |
@@ -198,10 +195,10 @@ public List<Document> similaritySearch(SearchRequest request) { |
198 | 195 | .build(), |
199 | 196 | Aggregation.match(new Criteria(SCORE_FIELD_NAME).gte(request.getSimilarityThreshold()))); |
200 | 197 |
|
201 | | - return this.mongoTemplate.aggregate(aggregation, this.config.collectionName, BasicDBObject.class) |
| 198 | + return this.mongoTemplate.aggregate(aggregation, this.config.collectionName, org.bson.Document.class) |
202 | 199 | .getMappedResults() |
203 | 200 | .stream() |
204 | | - .map(this::mapBasicDbObject) |
| 201 | + .map(this::mapMongoDocument) |
205 | 202 | .toList(); |
206 | 203 | } |
207 | 204 |
|
|
0 commit comments