Skip to content
Open
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 @@ -261,8 +261,11 @@ public void doAdd(List<Document> documents) {
List<float[]> embeddings = this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(),
this.batchingStrategy);
for (Document document : documents) {
MongoDBDocument mdbDocument = new MongoDBDocument(document.getId(), document.getText(),
document.getMetadata(), embeddings.get(documents.indexOf(document)));
org.bson.Document mdbDocument = new org.bson.Document();
mdbDocument.put(ID_FIELD_NAME, document.getId());
mdbDocument.put(CONTENT_FIELD_NAME, document.getText());
mdbDocument.put(METADATA_FIELD_NAME, document.getMetadata());
mdbDocument.put(this.pathName, EmbeddingUtils.toList(embeddings.get(documents.indexOf(document))));
this.mongoTemplate.save(mdbDocument, this.collectionName);
}
}
Expand Down Expand Up @@ -464,15 +467,4 @@ public MongoDBAtlasVectorStore build() {

}

/**
* The representation of {@link Document} along with its embedding.
*
* @param id The id of the document
* @param content The content of the document
* @param metadata The metadata of the document
* @param embedding The vectors representing the content of the document
*/
public record MongoDBDocument(String id, String content, Map<String, Object> metadata, float[] embedding) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,50 @@ void getNativeClientTest() {
});
}

@Test
void customPathNameTest() {
this.contextRunner.run(context -> {
MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class);

// Create a vector store with custom pathName
String customPathName = "custom_embedding";
String customCollectionName = "vector_store_custom_path";
MongoDBAtlasVectorStore vectorStore = MongoDBAtlasVectorStore.builder(mongoTemplate, embeddingModel)
.collectionName(customCollectionName)
.pathName(customPathName)
.initializeSchema(true)
.build();

// Clean up collection before test
mongoTemplate.getCollection(customCollectionName).deleteMany(new org.bson.Document());

// Add a document
Document document = new Document("Test content for custom pathName",
Collections.singletonMap("meta1", "value1"));
vectorStore.add(List.of(document));
Thread.sleep(5000); // Wait for indexing

// Verify the document was saved with the custom pathName
org.bson.Document savedDocument = mongoTemplate.findById(document.getId(), org.bson.Document.class,
customCollectionName);
assertThat(savedDocument).isNotNull();
assertThat(savedDocument.containsKey(customPathName)).isTrue();
assertThat(savedDocument.get(customPathName)).isNotNull();
assertThat(savedDocument.containsKey("embedding")).isFalse(); // Should not
// have
// default
// field name

// Verify similarity search still works with custom pathName
List<Document> results = vectorStore
.similaritySearch(SearchRequest.builder().query("Test content").topK(1).build());
assertThat(results).hasSize(1);
assertThat(results.get(0).getId()).isEqualTo(document.getId());
assertThat(results.get(0).getText()).isEqualTo("Test content for custom pathName");
});
}

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