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 @@ -240,7 +240,7 @@ private org.bson.Document createSearchIndexDefinition() {
* @return the Spring AI Document
*/
private Document mapMongoDocument(org.bson.Document mongoDocument, float[] queryEmbedding) {
String id = mongoDocument.getString(ID_FIELD_NAME);
String id = mongoDocument.getObjectId(ID_FIELD_NAME).toHexString();
String content = mongoDocument.getString(CONTENT_FIELD_NAME);
double score = mongoDocument.getDouble(SCORE_FIELD_NAME);
Map<String, Object> metadata = mongoDocument.get(METADATA_FIELD_NAME, org.bson.Document.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ void getNativeClientTest() {
});
}

@Test
void shouldHandleObjectIdInSearchResults() {
this.contextRunner.run(context -> {
VectorStore vectorStore = context.getBean(VectorStore.class);

Document doc1 = new Document("Test content for ObjectId handling");
Document doc2 = new Document("Another test content for ObjectId handling");
vectorStore.add(List.of(doc1, doc2));
Thread.sleep(5000);

List<Document> results = vectorStore
.similaritySearch(SearchRequest.builder().query("Test content").topK(5).build());

assertThat(results).hasSize(2);
results.forEach(result -> {
String id = result.getId();
assertThat(id).isNotNull();
assertThat(id).matches("^[0-9a-fA-F]{24}$");
assertThat(result.getText()).contains("Test content");
assertThat(result.getMetadata()).containsKey(DocumentMetadata.DISTANCE.value());
});
});
}

public static String getText(String uri) {
var resource = new DefaultResourceLoader().getResource(uri);
try {
Expand Down