From 5127662f2cce56669cb364034ec368683a435fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 21 Mar 2025 19:00:41 -0600 Subject: [PATCH] Use PropertyMapper in vector store auto-configurations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Eddú Meléndez --- .../CosmosDBVectorStoreAutoConfiguration.java | 7 +--- ...aseSearchVectorStoreAutoConfiguration.java | 33 ++++++----------- ...ticsearchVectorStoreAutoConfiguration.java | 18 +++------ .../MilvusVectorStoreAutoConfiguration.java | 37 +++++-------------- ...goDBAtlasVectorStoreAutoConfiguration.java | 22 +++-------- 5 files changed, 34 insertions(+), 83 deletions(-) diff --git a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-azure-cosmos-db/src/main/java/org/springframework/ai/vectorstore/cosmosdb/autoconfigure/CosmosDBVectorStoreAutoConfiguration.java b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-azure-cosmos-db/src/main/java/org/springframework/ai/vectorstore/cosmosdb/autoconfigure/CosmosDBVectorStoreAutoConfiguration.java index 5fdc1686b..b11a606a8 100644 --- a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-azure-cosmos-db/src/main/java/org/springframework/ai/vectorstore/cosmosdb/autoconfigure/CosmosDBVectorStoreAutoConfiguration.java +++ b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-azure-cosmos-db/src/main/java/org/springframework/ai/vectorstore/cosmosdb/autoconfigure/CosmosDBVectorStoreAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ import java.util.List; * {@link AutoConfiguration Auto-configuration} for CosmosDB Vector Store. * * @author Theo van Kraay + * @author Eddú Meléndez * @author Soby Chacko * @since 1.0.0 */ @@ -49,10 +50,6 @@ import java.util.List; matchIfMissing = true) public class CosmosDBVectorStoreAutoConfiguration { - String endpoint; - - String key; - @Bean public CosmosAsyncClient cosmosClient(CosmosDBVectorStoreProperties properties) { return new CosmosClientBuilder().endpoint(properties.getEndpoint()) diff --git a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-couchbase/src/main/java/org/springframework/ai/vectorstore/couchbase/autoconfigure/CouchbaseSearchVectorStoreAutoConfiguration.java b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-couchbase/src/main/java/org/springframework/ai/vectorstore/couchbase/autoconfigure/CouchbaseSearchVectorStoreAutoConfiguration.java index 96adb2a48..2e6a2b442 100644 --- a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-couchbase/src/main/java/org/springframework/ai/vectorstore/couchbase/autoconfigure/CouchbaseSearchVectorStoreAutoConfiguration.java +++ b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-couchbase/src/main/java/org/springframework/ai/vectorstore/couchbase/autoconfigure/CouchbaseSearchVectorStoreAutoConfiguration.java @@ -23,11 +23,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; -import org.springframework.util.StringUtils; /** * @author Laurent Doguin + * @author Eddú Meléndez * @since 1.0.0 */ @AutoConfiguration(after = CouchbaseAutoConfiguration.class) @@ -41,27 +42,15 @@ public class CouchbaseSearchVectorStoreAutoConfiguration { EmbeddingModel embeddingModel) { var builder = CouchbaseSearchVectorStore.builder(cluster, embeddingModel); - if (StringUtils.hasText(properties.getIndexName())) { - builder.vectorIndexName(properties.getIndexName()); - } - if (StringUtils.hasText(properties.getBucketName())) { - builder.bucketName(properties.getBucketName()); - } - if (StringUtils.hasText(properties.getScopeName())) { - builder.scopeName(properties.getScopeName()); - } - if (StringUtils.hasText(properties.getCollectionName())) { - builder.collectionName(properties.getCollectionName()); - } - if (properties.getDimensions() != null) { - builder.dimensions(properties.getDimensions()); - } - if (properties.getSimilarity() != null) { - builder.similarityFunction(properties.getSimilarity()); - } - if (properties.getOptimization() != null) { - builder.indexOptimization(properties.getOptimization()); - } + PropertyMapper mapper = PropertyMapper.get(); + mapper.from(properties::getIndexName).whenHasText().to(builder::vectorIndexName); + mapper.from(properties::getBucketName).whenHasText().to(builder::bucketName); + mapper.from(properties::getScopeName).whenHasText().to(builder::scopeName); + mapper.from(properties::getCollectionName).whenHasText().to(builder::collectionName); + mapper.from(properties::getDimensions).whenNonNull().to(builder::dimensions); + mapper.from(properties::getSimilarity).whenNonNull().to(builder::similarityFunction); + mapper.from(properties::getOptimization).whenNonNull().to(builder::indexOptimization); + return builder.initializeSchema(properties.isInitializeSchema()).build(); } diff --git a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-elasticsearch/src/main/java/org/springframework/ai/vectorstore/elasticsearch/autoconfigure/ElasticsearchVectorStoreAutoConfiguration.java b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-elasticsearch/src/main/java/org/springframework/ai/vectorstore/elasticsearch/autoconfigure/ElasticsearchVectorStoreAutoConfiguration.java index 56fc50c54..19f8d36b8 100644 --- a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-elasticsearch/src/main/java/org/springframework/ai/vectorstore/elasticsearch/autoconfigure/ElasticsearchVectorStoreAutoConfiguration.java +++ b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-elasticsearch/src/main/java/org/springframework/ai/vectorstore/elasticsearch/autoconfigure/ElasticsearchVectorStoreAutoConfiguration.java @@ -33,8 +33,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; -import org.springframework.util.StringUtils; /** * {@link AutoConfiguration Auto-configuration} for Elasticsearch Vector Store. @@ -68,18 +68,10 @@ public class ElasticsearchVectorStoreAutoConfiguration { BatchingStrategy batchingStrategy) { ElasticsearchVectorStoreOptions elasticsearchVectorStoreOptions = new ElasticsearchVectorStoreOptions(); - if (StringUtils.hasText(properties.getIndexName())) { - elasticsearchVectorStoreOptions.setIndexName(properties.getIndexName()); - } - if (properties.getDimensions() != null) { - elasticsearchVectorStoreOptions.setDimensions(properties.getDimensions()); - } - if (properties.getSimilarity() != null) { - elasticsearchVectorStoreOptions.setSimilarity(properties.getSimilarity()); - } - if (properties.getEmbeddingFieldName() != null) { - elasticsearchVectorStoreOptions.setEmbeddingFieldName(properties.getEmbeddingFieldName()); - } + PropertyMapper mapper = PropertyMapper.get(); + mapper.from(properties::getIndexName).whenHasText().to(elasticsearchVectorStoreOptions::setIndexName); + mapper.from(properties::getDimensions).whenNonNull().to(elasticsearchVectorStoreOptions::setDimensions); + mapper.from(properties::getSimilarity).whenNonNull().to(elasticsearchVectorStoreOptions::setSimilarity); return ElasticsearchVectorStore.builder(restClient, embeddingModel) .options(elasticsearchVectorStoreOptions) diff --git a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-milvus/src/main/java/org/springframework/ai/vectorstore/milvus/autoconfigure/MilvusVectorStoreAutoConfiguration.java b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-milvus/src/main/java/org/springframework/ai/vectorstore/milvus/autoconfigure/MilvusVectorStoreAutoConfiguration.java index 88830487e..7dda6426d 100644 --- a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-milvus/src/main/java/org/springframework/ai/vectorstore/milvus/autoconfigure/MilvusVectorStoreAutoConfiguration.java +++ b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-milvus/src/main/java/org/springframework/ai/vectorstore/milvus/autoconfigure/MilvusVectorStoreAutoConfiguration.java @@ -36,8 +36,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; -import org.springframework.util.StringUtils; /** * {@link AutoConfiguration Auto-configuration} for Milvus Vector Store. @@ -110,32 +110,15 @@ public class MilvusVectorStoreAutoConfiguration { .withIdleTimeout(clientProperties.getIdleTimeoutMs(), TimeUnit.MILLISECONDS) .withAuthorization(clientProperties.getUsername(), clientProperties.getPassword()); - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getUri())) { - builder.withUri(clientProperties.getUri()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getToken())) { - builder.withToken(clientProperties.getToken()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getClientKeyPath())) { - builder.withClientKeyPath(clientProperties.getClientKeyPath()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getClientPemPath())) { - builder.withClientPemPath(clientProperties.getClientPemPath()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getCaPemPath())) { - builder.withCaPemPath(clientProperties.getCaPemPath()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getServerPemPath())) { - builder.withServerPemPath(clientProperties.getServerPemPath()); - } - - if (clientProperties.isSecure() && StringUtils.hasText(clientProperties.getServerName())) { - builder.withServerName(clientProperties.getServerName()); + if (clientProperties.isSecure()) { + PropertyMapper mapper = PropertyMapper.get(); + mapper.from(clientProperties::getUri).whenHasText().to(builder::withUri); + mapper.from(clientProperties::getToken).whenHasText().to(builder::withToken); + mapper.from(clientProperties::getClientKeyPath).whenHasText().to(builder::withClientKeyPath); + mapper.from(clientProperties::getClientPemPath).whenHasText().to(builder::withClientPemPath); + mapper.from(clientProperties::getCaPemPath).whenHasText().to(builder::withCaPemPath); + mapper.from(clientProperties::getServerPemPath).whenHasText().to(builder::withServerPemPath); + mapper.from(clientProperties::getServerName).whenHasText().to(builder::withServerName); } return new MilvusServiceClient(builder.build()); diff --git a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-mongodb-atlas/src/main/java/org/springframework/ai/vectorstore/mongodb/autoconfigure/MongoDBAtlasVectorStoreAutoConfiguration.java b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-mongodb-atlas/src/main/java/org/springframework/ai/vectorstore/mongodb/autoconfigure/MongoDBAtlasVectorStoreAutoConfiguration.java index 4aa8aeb7c..001e84e92 100644 --- a/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-mongodb-atlas/src/main/java/org/springframework/ai/vectorstore/mongodb/autoconfigure/MongoDBAtlasVectorStoreAutoConfiguration.java +++ b/auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-mongodb-atlas/src/main/java/org/springframework/ai/vectorstore/mongodb/autoconfigure/MongoDBAtlasVectorStoreAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,13 +33,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.convert.MongoCustomConversions; import org.springframework.util.CollectionUtils; import org.springframework.util.MimeType; -import org.springframework.util.StringUtils; /** * {@link AutoConfiguration Auto-configuration} for MongoDB Atlas Vector Store. @@ -76,20 +76,10 @@ public class MongoDBAtlasVectorStoreAutoConfiguration { .customObservationConvention(customObservationConvention.getIfAvailable(() -> null)) .batchingStrategy(batchingStrategy); - String collectionName = properties.getCollectionName(); - if (StringUtils.hasText(collectionName)) { - builder.collectionName(collectionName); - } - - String pathName = properties.getPathName(); - if (StringUtils.hasText(pathName)) { - builder.pathName(pathName); - } - - String indexName = properties.getIndexName(); - if (StringUtils.hasText(indexName)) { - builder.vectorIndexName(indexName); - } + PropertyMapper mapper = PropertyMapper.get(); + mapper.from(properties::getCollectionName).whenHasText().to(builder::collectionName); + mapper.from(properties::getPathName).whenHasText().to(builder::pathName); + mapper.from(properties::getIndexName).whenHasText().to(builder::vectorIndexName); List metadataFields = properties.getMetadataFieldsToFilter(); if (!CollectionUtils.isEmpty(metadataFields)) {