Skip to content

Commit 5127662

Browse files
eddumelendezsobychacko
authored andcommitted
Use PropertyMapper in vector store auto-configurations
Signed-off-by: Eddú Meléndez <eddu.melendez@gmail.com>
1 parent 4be1002 commit 5127662

File tree

5 files changed

+34
-83
lines changed

5 files changed

+34
-83
lines changed

auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-azure-cosmos-db/src/main/java/org/springframework/ai/vectorstore/cosmosdb/autoconfigure/CosmosDBVectorStoreAutoConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
* {@link AutoConfiguration Auto-configuration} for CosmosDB Vector Store.
4040
*
4141
* @author Theo van Kraay
42+
* @author Eddú Meléndez
4243
* @author Soby Chacko
4344
* @since 1.0.0
4445
*/
@@ -49,10 +50,6 @@
4950
matchIfMissing = true)
5051
public class CosmosDBVectorStoreAutoConfiguration {
5152

52-
String endpoint;
53-
54-
String key;
55-
5653
@Bean
5754
public CosmosAsyncClient cosmosClient(CosmosDBVectorStoreProperties properties) {
5855
return new CosmosClientBuilder().endpoint(properties.getEndpoint())

auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-couchbase/src/main/java/org/springframework/ai/vectorstore/couchbase/autoconfigure/CouchbaseSearchVectorStoreAutoConfiguration.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2424
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.boot.context.properties.PropertyMapper;
2627
import org.springframework.context.annotation.Bean;
27-
import org.springframework.util.StringUtils;
2828

2929
/**
3030
* @author Laurent Doguin
31+
* @author Eddú Meléndez
3132
* @since 1.0.0
3233
*/
3334
@AutoConfiguration(after = CouchbaseAutoConfiguration.class)
@@ -41,27 +42,15 @@ public CouchbaseSearchVectorStore vectorStore(CouchbaseSearchVectorStoreProperti
4142
EmbeddingModel embeddingModel) {
4243
var builder = CouchbaseSearchVectorStore.builder(cluster, embeddingModel);
4344

44-
if (StringUtils.hasText(properties.getIndexName())) {
45-
builder.vectorIndexName(properties.getIndexName());
46-
}
47-
if (StringUtils.hasText(properties.getBucketName())) {
48-
builder.bucketName(properties.getBucketName());
49-
}
50-
if (StringUtils.hasText(properties.getScopeName())) {
51-
builder.scopeName(properties.getScopeName());
52-
}
53-
if (StringUtils.hasText(properties.getCollectionName())) {
54-
builder.collectionName(properties.getCollectionName());
55-
}
56-
if (properties.getDimensions() != null) {
57-
builder.dimensions(properties.getDimensions());
58-
}
59-
if (properties.getSimilarity() != null) {
60-
builder.similarityFunction(properties.getSimilarity());
61-
}
62-
if (properties.getOptimization() != null) {
63-
builder.indexOptimization(properties.getOptimization());
64-
}
45+
PropertyMapper mapper = PropertyMapper.get();
46+
mapper.from(properties::getIndexName).whenHasText().to(builder::vectorIndexName);
47+
mapper.from(properties::getBucketName).whenHasText().to(builder::bucketName);
48+
mapper.from(properties::getScopeName).whenHasText().to(builder::scopeName);
49+
mapper.from(properties::getCollectionName).whenHasText().to(builder::collectionName);
50+
mapper.from(properties::getDimensions).whenNonNull().to(builder::dimensions);
51+
mapper.from(properties::getSimilarity).whenNonNull().to(builder::similarityFunction);
52+
mapper.from(properties::getOptimization).whenNonNull().to(builder::indexOptimization);
53+
6554
return builder.initializeSchema(properties.isInitializeSchema()).build();
6655
}
6756

auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-elasticsearch/src/main/java/org/springframework/ai/vectorstore/elasticsearch/autoconfigure/ElasticsearchVectorStoreAutoConfiguration.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3434
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
3535
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.boot.context.properties.PropertyMapper;
3637
import org.springframework.context.annotation.Bean;
37-
import org.springframework.util.StringUtils;
3838

3939
/**
4040
* {@link AutoConfiguration Auto-configuration} for Elasticsearch Vector Store.
@@ -68,18 +68,10 @@ ElasticsearchVectorStore vectorStore(ElasticsearchVectorStoreProperties properti
6868
BatchingStrategy batchingStrategy) {
6969
ElasticsearchVectorStoreOptions elasticsearchVectorStoreOptions = new ElasticsearchVectorStoreOptions();
7070

71-
if (StringUtils.hasText(properties.getIndexName())) {
72-
elasticsearchVectorStoreOptions.setIndexName(properties.getIndexName());
73-
}
74-
if (properties.getDimensions() != null) {
75-
elasticsearchVectorStoreOptions.setDimensions(properties.getDimensions());
76-
}
77-
if (properties.getSimilarity() != null) {
78-
elasticsearchVectorStoreOptions.setSimilarity(properties.getSimilarity());
79-
}
80-
if (properties.getEmbeddingFieldName() != null) {
81-
elasticsearchVectorStoreOptions.setEmbeddingFieldName(properties.getEmbeddingFieldName());
82-
}
71+
PropertyMapper mapper = PropertyMapper.get();
72+
mapper.from(properties::getIndexName).whenHasText().to(elasticsearchVectorStoreOptions::setIndexName);
73+
mapper.from(properties::getDimensions).whenNonNull().to(elasticsearchVectorStoreOptions::setDimensions);
74+
mapper.from(properties::getSimilarity).whenNonNull().to(elasticsearchVectorStoreOptions::setSimilarity);
8375

8476
return ElasticsearchVectorStore.builder(restClient, embeddingModel)
8577
.options(elasticsearchVectorStoreOptions)

auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-milvus/src/main/java/org/springframework/ai/vectorstore/milvus/autoconfigure/MilvusVectorStoreAutoConfiguration.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3737
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3838
import org.springframework.boot.context.properties.EnableConfigurationProperties;
39+
import org.springframework.boot.context.properties.PropertyMapper;
3940
import org.springframework.context.annotation.Bean;
40-
import org.springframework.util.StringUtils;
4141

4242
/**
4343
* {@link AutoConfiguration Auto-configuration} for Milvus Vector Store.
@@ -110,32 +110,15 @@ public MilvusServiceClient milvusClient(MilvusVectorStoreProperties serverProper
110110
.withIdleTimeout(clientProperties.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
111111
.withAuthorization(clientProperties.getUsername(), clientProperties.getPassword());
112112

113-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getUri())) {
114-
builder.withUri(clientProperties.getUri());
115-
}
116-
117-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getToken())) {
118-
builder.withToken(clientProperties.getToken());
119-
}
120-
121-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getClientKeyPath())) {
122-
builder.withClientKeyPath(clientProperties.getClientKeyPath());
123-
}
124-
125-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getClientPemPath())) {
126-
builder.withClientPemPath(clientProperties.getClientPemPath());
127-
}
128-
129-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getCaPemPath())) {
130-
builder.withCaPemPath(clientProperties.getCaPemPath());
131-
}
132-
133-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getServerPemPath())) {
134-
builder.withServerPemPath(clientProperties.getServerPemPath());
135-
}
136-
137-
if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getServerName())) {
138-
builder.withServerName(clientProperties.getServerName());
113+
if (clientProperties.isSecure()) {
114+
PropertyMapper mapper = PropertyMapper.get();
115+
mapper.from(clientProperties::getUri).whenHasText().to(builder::withUri);
116+
mapper.from(clientProperties::getToken).whenHasText().to(builder::withToken);
117+
mapper.from(clientProperties::getClientKeyPath).whenHasText().to(builder::withClientKeyPath);
118+
mapper.from(clientProperties::getClientPemPath).whenHasText().to(builder::withClientPemPath);
119+
mapper.from(clientProperties::getCaPemPath).whenHasText().to(builder::withCaPemPath);
120+
mapper.from(clientProperties::getServerPemPath).whenHasText().to(builder::withServerPemPath);
121+
mapper.from(clientProperties::getServerName).whenHasText().to(builder::withServerName);
139122
}
140123

141124
return new MilvusServiceClient(builder.build());

auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-mongodb-atlas/src/main/java/org/springframework/ai/vectorstore/mongodb/autoconfigure/MongoDBAtlasVectorStoreAutoConfiguration.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,13 +33,13 @@
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3535
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.boot.context.properties.PropertyMapper;
3637
import org.springframework.context.annotation.Bean;
3738
import org.springframework.core.convert.converter.Converter;
3839
import org.springframework.data.mongodb.core.MongoTemplate;
3940
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
4041
import org.springframework.util.CollectionUtils;
4142
import org.springframework.util.MimeType;
42-
import org.springframework.util.StringUtils;
4343

4444
/**
4545
* {@link AutoConfiguration Auto-configuration} for MongoDB Atlas Vector Store.
@@ -76,20 +76,10 @@ MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel
7676
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
7777
.batchingStrategy(batchingStrategy);
7878

79-
String collectionName = properties.getCollectionName();
80-
if (StringUtils.hasText(collectionName)) {
81-
builder.collectionName(collectionName);
82-
}
83-
84-
String pathName = properties.getPathName();
85-
if (StringUtils.hasText(pathName)) {
86-
builder.pathName(pathName);
87-
}
88-
89-
String indexName = properties.getIndexName();
90-
if (StringUtils.hasText(indexName)) {
91-
builder.vectorIndexName(indexName);
92-
}
79+
PropertyMapper mapper = PropertyMapper.get();
80+
mapper.from(properties::getCollectionName).whenHasText().to(builder::collectionName);
81+
mapper.from(properties::getPathName).whenHasText().to(builder::pathName);
82+
mapper.from(properties::getIndexName).whenHasText().to(builder::vectorIndexName);
9383

9484
List<String> metadataFields = properties.getMetadataFieldsToFilter();
9585
if (!CollectionUtils.isEmpty(metadataFields)) {

0 commit comments

Comments
 (0)