Skip to content

Commit 2d3bdcd

Browse files
sobychackomarkpollack
authored andcommitted
Add builder pattern to MongoDBAtlasVectorStore and refactor package name
The MongoDBAtlasVectorStore implementation has been enhanced with a builder pattern to provide a more flexible and type-safe way to configure the vector store. This change improves the developer experience by making the API more intuitive and less error-prone. The old constructors and configuration classes have been deprecated in favor of the builder pattern. This aligns with Spring's best practices for configuration APIs. Additionally, the package has been refactored to org.springframework.ai.vectorstore.mongodb.atlas to avoid having multiple vector store modules share the same package name. Documentation has been updated to reflect these changes and provide examples of using the new builder pattern.review
1 parent 03a9379 commit 2d3bdcd

File tree

10 files changed

+460
-221
lines changed

10 files changed

+460
-221
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc

Lines changed: 131 additions & 162 deletions
Large diffs are not rendered by default.

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/mongo/MongoDBAtlasVectorStoreAutoConfiguration.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package org.springframework.ai.autoconfigure.vectorstore.mongo;
1818

1919
import java.util.Arrays;
20+
import java.util.List;
2021

2122
import io.micrometer.observation.ObservationRegistry;
2223

2324
import org.springframework.ai.embedding.BatchingStrategy;
2425
import org.springframework.ai.embedding.EmbeddingModel;
2526
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
26-
import org.springframework.ai.vectorstore.MongoDBAtlasVectorStore;
27+
import org.springframework.ai.vectorstore.mongodb.atlas.MongoDBAtlasVectorStore;
2728
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
2829
import org.springframework.beans.factory.ObjectProvider;
2930
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -34,6 +35,7 @@
3435
import org.springframework.core.convert.converter.Converter;
3536
import org.springframework.data.mongodb.core.MongoTemplate;
3637
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
38+
import org.springframework.util.CollectionUtils;
3739
import org.springframework.util.MimeType;
3840
import org.springframework.util.StringUtils;
3941

@@ -64,25 +66,35 @@ MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel
6466
ObjectProvider<VectorStoreObservationConvention> customObservationConvention,
6567
BatchingStrategy batchingStrategy) {
6668

67-
var builder = MongoDBAtlasVectorStore.MongoDBVectorStoreConfig.builder();
68-
69-
if (StringUtils.hasText(properties.getCollectionName())) {
70-
builder.withCollectionName(properties.getCollectionName());
69+
MongoDBAtlasVectorStore.MongoDBBuilder builder = MongoDBAtlasVectorStore.builder()
70+
.mongoTemplate(mongoTemplate)
71+
.embeddingModel(embeddingModel)
72+
.initializeSchema(properties.isInitializeSchema())
73+
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
74+
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
75+
.batchingStrategy(batchingStrategy);
76+
77+
String collectionName = properties.getCollectionName();
78+
if (StringUtils.hasText(collectionName)) {
79+
builder.collectionName(collectionName);
7180
}
72-
if (StringUtils.hasText(properties.getPathName())) {
73-
builder.withPathName(properties.getPathName());
81+
82+
String pathName = properties.getPathName();
83+
if (StringUtils.hasText(pathName)) {
84+
builder.pathName(pathName);
7485
}
75-
if (StringUtils.hasText(properties.getIndexName())) {
76-
builder.withVectorIndexName(properties.getIndexName());
86+
87+
String indexName = properties.getIndexName();
88+
if (StringUtils.hasText(indexName)) {
89+
builder.vectorIndexName(indexName);
7790
}
78-
if (!properties.getMetadataFieldsToFilter().isEmpty()) {
79-
builder.withMetadataFieldsToFilter(properties.getMetadataFieldsToFilter());
91+
92+
List<String> metadataFields = properties.getMetadataFieldsToFilter();
93+
if (!CollectionUtils.isEmpty(metadataFields)) {
94+
builder.metadataFieldsToFilter(metadataFields);
8095
}
81-
MongoDBAtlasVectorStore.MongoDBVectorStoreConfig config = builder.build();
8296

83-
return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel, config, properties.isInitializeSchema(),
84-
observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
85-
customObservationConvention.getIfAvailable(() -> null), batchingStrategy);
97+
return builder.build();
8698
}
8799

88100
@Bean
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.vectorstore;
17+
package org.springframework.ai.vectorstore.mongodb.atlas;
1818

1919
import org.springframework.ai.vectorstore.filter.Filter;
2020
import org.springframework.ai.vectorstore.filter.converter.AbstractFilterExpressionConverter;

0 commit comments

Comments
 (0)