Skip to content

Commit 7149dde

Browse files
committed
Use PropertyMapper in vector store auto-configurations
1 parent 29002df commit 7149dde

File tree

5 files changed

+37
-83
lines changed

5 files changed

+37
-83
lines changed

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/cosmosdb/CosmosDBVectorStoreAutoConfiguration.java

+2-5
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.
@@ -37,6 +37,7 @@
3737
* {@link AutoConfiguration Auto-configuration} for CosmosDB Vector Store.
3838
*
3939
* @author Theo van Kraay
40+
* @author Eddú Meléndez
4041
* @author Soby Chacko
4142
* @since 1.0.0
4243
*/
@@ -45,10 +46,6 @@
4546
@EnableConfigurationProperties(CosmosDBVectorStoreProperties.class)
4647
public class CosmosDBVectorStoreAutoConfiguration {
4748

48-
String endpoint;
49-
50-
String key;
51-
5249
@Bean
5350
public CosmosAsyncClient cosmosClient(CosmosDBVectorStoreProperties properties) {
5451
return new CosmosClientBuilder().endpoint(properties.getEndpoint())

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/couchbase/CouchbaseSearchVectorStoreAutoConfiguration.java

+12-23
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.
@@ -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

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/elasticsearch/ElasticsearchVectorStoreAutoConfiguration.java

+6-11
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.
@@ -31,8 +31,8 @@
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3232
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
3333
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34+
import org.springframework.boot.context.properties.PropertyMapper;
3435
import org.springframework.context.annotation.Bean;
35-
import org.springframework.util.StringUtils;
3636

3737
/**
3838
* {@link AutoConfiguration Auto-configuration} for Elasticsearch Vector Store.
@@ -63,15 +63,10 @@ ElasticsearchVectorStore vectorStore(ElasticsearchVectorStoreProperties properti
6363
BatchingStrategy batchingStrategy) {
6464
ElasticsearchVectorStoreOptions elasticsearchVectorStoreOptions = new ElasticsearchVectorStoreOptions();
6565

66-
if (StringUtils.hasText(properties.getIndexName())) {
67-
elasticsearchVectorStoreOptions.setIndexName(properties.getIndexName());
68-
}
69-
if (properties.getDimensions() != null) {
70-
elasticsearchVectorStoreOptions.setDimensions(properties.getDimensions());
71-
}
72-
if (properties.getSimilarity() != null) {
73-
elasticsearchVectorStoreOptions.setSimilarity(properties.getSimilarity());
74-
}
66+
PropertyMapper mapper = PropertyMapper.get();
67+
mapper.from(properties::getIndexName).whenHasText().to(elasticsearchVectorStoreOptions::setIndexName);
68+
mapper.from(properties::getDimensions).whenNonNull().to(elasticsearchVectorStoreOptions::setDimensions);
69+
mapper.from(properties::getSimilarity).whenNonNull().to(elasticsearchVectorStoreOptions::setSimilarity);
7570

7671
return ElasticsearchVectorStore.builder(restClient, embeddingModel)
7772
.options(elasticsearchVectorStoreOptions)

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/milvus/MilvusVectorStoreAutoConfiguration.java

+11-28
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.
@@ -34,8 +34,8 @@
3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3636
import org.springframework.boot.context.properties.EnableConfigurationProperties;
37+
import org.springframework.boot.context.properties.PropertyMapper;
3738
import org.springframework.context.annotation.Bean;
38-
import org.springframework.util.StringUtils;
3939

4040
/**
4141
* {@link AutoConfiguration Auto-configuration} for Milvus Vector Store.
@@ -106,32 +106,15 @@ public MilvusServiceClient milvusClient(MilvusVectorStoreProperties serverProper
106106
.withIdleTimeout(clientProperties.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
107107
.withAuthorization(clientProperties.getUsername(), clientProperties.getPassword());
108108

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

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

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

+6-16
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.
@@ -31,13 +31,13 @@
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3333
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34+
import org.springframework.boot.context.properties.PropertyMapper;
3435
import org.springframework.context.annotation.Bean;
3536
import org.springframework.core.convert.converter.Converter;
3637
import org.springframework.data.mongodb.core.MongoTemplate;
3738
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
3839
import org.springframework.util.CollectionUtils;
3940
import org.springframework.util.MimeType;
40-
import org.springframework.util.StringUtils;
4141

4242
/**
4343
* {@link AutoConfiguration Auto-configuration} for MongoDB Atlas Vector Store.
@@ -72,20 +72,10 @@ MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel
7272
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
7373
.batchingStrategy(batchingStrategy);
7474

75-
String collectionName = properties.getCollectionName();
76-
if (StringUtils.hasText(collectionName)) {
77-
builder.collectionName(collectionName);
78-
}
79-
80-
String pathName = properties.getPathName();
81-
if (StringUtils.hasText(pathName)) {
82-
builder.pathName(pathName);
83-
}
84-
85-
String indexName = properties.getIndexName();
86-
if (StringUtils.hasText(indexName)) {
87-
builder.vectorIndexName(indexName);
88-
}
75+
PropertyMapper mapper = PropertyMapper.get();
76+
mapper.from(properties::getCollectionName).whenHasText().to(builder::collectionName);
77+
mapper.from(properties::getPathName).whenHasText().to(builder::pathName);
78+
mapper.from(properties::getIndexName).whenHasText().to(builder::vectorIndexName);
8979

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

0 commit comments

Comments
 (0)