From c8abc65a517cb6f3f6aeb35bf0dd4713f7429d0d Mon Sep 17 00:00:00 2001 From: xgc Date: Sun, 12 May 2024 02:38:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=89=AB=E6=8F=8F?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=8C=85=E5=AE=9E=E4=BD=93=E7=B1=BB=E7=BC=93?= =?UTF-8?q?=E5=AD=98+=E8=87=AA=E5=8A=A8=E6=9E=84=E5=BB=BA=E9=9B=86?= =?UTF-8?q?=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../milvus/demo/MilvusDemoApplication.java | 3 + .../plus/builder/CollectionSchemaBuilder.java | 12 +- .../milvus/plus/cache/PropertyCache.java | 10 ++ .../plus/config/MilvusCollectionConfig.java | 40 ----- .../milvus/plus/config/MilvusConfig.java | 5 +- .../milvus/plus/config/MilvusInit.java | 139 ++++++++++++++++++ .../milvus/plus/config/MilvusProperties.java | 4 + .../plus/converter/MilvusEntityConverter.java | 37 +++-- .../plus/converter/SearchRespConverter.java | 23 ++- .../core/conditions/LambdaDeleteWrapper.java | 10 +- .../core/conditions/LambdaInsertWrapper.java | 10 +- .../core/conditions/LambdaSearchWrapper.java | 13 +- .../core/conditions/LambdaUpdateWrapper.java | 12 +- .../milvus/plus/core/conditions/Wrapper.java | 4 +- .../milvus/plus/core/mapper/MilvusMapper.java | 4 +- .../milvus/plus/service/MilvusClient.java | 19 --- .../plus/service/MilvusCollectionService.java | 77 ---------- 17 files changed, 241 insertions(+), 181 deletions(-) delete mode 100644 milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusCollectionConfig.java create mode 100644 milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusInit.java delete mode 100644 milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusClient.java delete mode 100644 milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusCollectionService.java diff --git a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/MilvusDemoApplication.java b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/MilvusDemoApplication.java index a10ac2d..3648eb4 100644 --- a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/MilvusDemoApplication.java +++ b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/MilvusDemoApplication.java @@ -2,6 +2,9 @@ package io.github.javpower.milvus.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; @SpringBootApplication public class MilvusDemoApplication { diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/builder/CollectionSchemaBuilder.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/builder/CollectionSchemaBuilder.java index a02eafe..036479b 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/builder/CollectionSchemaBuilder.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/builder/CollectionSchemaBuilder.java @@ -1,7 +1,7 @@ package io.github.javpower.milvus.plus.builder; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.common.IndexParam; import io.milvus.v2.service.collection.request.AddFieldReq; import io.milvus.v2.service.collection.request.CreateCollectionReq; @@ -12,13 +12,13 @@ import io.milvus.v2.service.index.request.CreateIndexReq; public class CollectionSchemaBuilder { private final String collectionName; - private final MilvusClient wrapper; + private final MilvusClientV2 wrapper; private final CreateCollectionReq.CollectionSchema schema; - public CollectionSchemaBuilder(String collectionName, MilvusClient wrapper) { + public CollectionSchemaBuilder(String collectionName, MilvusClientV2 wrapper) { this.collectionName = collectionName; this.wrapper = wrapper; - this.schema = wrapper.client.createSchema(); + this.schema = wrapper.createSchema(); } public CollectionSchemaBuilder addField(AddFieldReq field) { @@ -34,13 +34,13 @@ public class CollectionSchemaBuilder { public void createSchema() throws MilvusException { CreateCollectionReq req=CreateCollectionReq.builder().collectionName(this.collectionName).collectionSchema(this.schema).build(); - wrapper.client.createCollection(req); + wrapper.createCollection(req); } public void createIndex(java.util.List indexParams) throws MilvusException { CreateIndexReq req = CreateIndexReq.builder() .collectionName(collectionName) .indexParams(indexParams) .build(); - wrapper.client.createIndex(req); + wrapper.createIndex(req); } } \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java index cf00224..7423a28 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java @@ -9,4 +9,14 @@ public class PropertyCache { public Map functionToPropertyMap = new HashMap<>(); //属性名称->表名称 + + // 根据值查找第一个匹配的键 + public String findKeyByValue(String value) { + for (Map.Entry entry : functionToPropertyMap.entrySet()) { + if (value.equals(entry.getValue())) { + return entry.getKey(); // 返回与值匹配的第一个键 + } + } + return null; // 如果没有找到匹配的键,返回null + } } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusCollectionConfig.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusCollectionConfig.java deleted file mode 100644 index 4a7646d..0000000 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusCollectionConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -package io.github.javpower.milvus.plus.config; - -import io.github.javpower.milvus.plus.annotation.MilvusCollection; -import io.github.javpower.milvus.plus.service.MilvusCollectionService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; - -import java.util.Arrays; - -/** - * @author xgc - **/ -@Component -public class MilvusCollectionConfig implements ApplicationRunner { - - private final ApplicationContext applicationContext; - private final MilvusCollectionService milvusCollectionService; - - @Autowired(required = false) - public MilvusCollectionConfig(ApplicationContext applicationContext, MilvusCollectionService milvusCollectionService) { - this.applicationContext = applicationContext; - this.milvusCollectionService = milvusCollectionService; - } - - @Override - public void run(ApplicationArguments args) throws Exception { - // 获取所有带有@MilvusCollection注解的类 - String[] beanNames = applicationContext.getBeanNamesForAnnotation(MilvusCollection.class); - Class[] annotatedClasses = Arrays.stream(beanNames) - .map(applicationContext::getType) - .toArray(Class[]::new); - // 调用业务处理服务 - if(milvusCollectionService!=null){ - milvusCollectionService.performBusinessLogic(Arrays.asList(annotatedClasses)); - } - } -} \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusConfig.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusConfig.java index 27b71fc..0531b67 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusConfig.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusConfig.java @@ -4,11 +4,13 @@ import io.milvus.v2.client.ConnectConfig; import io.milvus.v2.client.MilvusClientV2; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; + /** * @author xgc **/ @Configuration public class MilvusConfig { + private final MilvusProperties properties; public MilvusConfig(MilvusProperties properties) { @@ -22,8 +24,9 @@ public class MilvusConfig { } ConnectConfig connectConfig = ConnectConfig.builder() .uri(properties.getUri()) - .token(properties.getToken()) + // .token(properties.getToken()) .build(); return new MilvusClientV2(connectConfig); } + } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusInit.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusInit.java new file mode 100644 index 0000000..90fc154 --- /dev/null +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusInit.java @@ -0,0 +1,139 @@ +package io.github.javpower.milvus.plus.config; + + +import io.github.javpower.milvus.plus.annotation.MilvusCollection; +import io.github.javpower.milvus.plus.builder.CollectionSchemaBuilder; +import io.github.javpower.milvus.plus.converter.MilvusEntityConverter; +import io.github.javpower.milvus.plus.model.MilvusEntity; +import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; +import io.milvus.v2.common.IndexParam; +import io.milvus.v2.service.collection.request.*; +import io.milvus.v2.service.collection.response.DescribeCollectionResp; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.core.type.classreading.CachingMetadataReaderFactory; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.stereotype.Service; +import org.springframework.util.ClassUtils; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author xgc + **/ +@Service +@Slf4j +public class MilvusInit implements AutoCloseable { + private final static String CLASS="*.class"; + + public final MilvusClientV2 client; + + public MilvusInit(MilvusClientV2 client, MilvusProperties properties) { + this.client = client; + List> classes = getClass(properties.getPackages()); + performBusinessLogic(classes); + } + + @Override + public void close() throws InterruptedException { + client.close(10); + } + //获取指定包下实体类 + private List> getClass(List packages){ + List> res=new ArrayList<>(); + ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); + for (String pg : packages) { + String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + + ClassUtils.convertClassNameToResourcePath(pg+".") + CLASS; + try { + Resource[] resources = resourcePatternResolver.getResources(pattern); + MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver); + for (Resource resource : resources) { + MetadataReader reader = readerFactory.getMetadataReader(resource); + String classname = reader.getClassMetadata().getClassName(); + Class clazz = Class.forName(classname); + MilvusCollection annotation = clazz.getAnnotation(MilvusCollection.class); + if(annotation!=null){ + res.add(clazz); + } + } + }catch (Exception e){ + e.printStackTrace(); + } + } + return res; + } + //缓存+是否构建集合 + public void performBusinessLogic(List> annotatedClasses) { + for (Class milvusClass : annotatedClasses) { + MilvusEntity milvusEntity = MilvusEntityConverter.convert(milvusClass); + try { + String collectionName = milvusEntity.getCollectionName(); + // 检查集合是否存在 + boolean collectionExists = client.hasCollection( + HasCollectionReq.builder().collectionName(collectionName).build() + ); + if (collectionExists) { + // 获取集合的详细信息 + DescribeCollectionResp collectionInfo = client.describeCollection( + DescribeCollectionReq.builder().collectionName(collectionName).build() + ); + // 检查字段是否一致,这里需要实现字段比较逻辑 + List existingFieldNames = collectionInfo.getFieldNames(); + List requiredFields = milvusEntity.getMilvusFields(); + List requiredFieldNames = requiredFields.stream().map(AddFieldReq::getFieldName).collect(Collectors.toList()); + if (!new HashSet<>(existingFieldNames).containsAll(requiredFieldNames) || !new HashSet<>(requiredFieldNames).containsAll(existingFieldNames)) { + // 字段不一致,删除并重新创建集合 + client.dropCollection( + DropCollectionReq.builder().collectionName(collectionName).build() + ); + // 创建新集合 + create(milvusEntity,client); + loadStatus(collectionName); + } + } else { + // 创建新集合 + create(milvusEntity,client); + loadStatus(collectionName); + } + } catch (MilvusException e) { + throw new RuntimeException("Error handling Milvus collection", e); + } + } + } + private void create(MilvusEntity milvusEntity,MilvusClientV2 client){ + // 创建新集合 + CollectionSchemaBuilder schemaBuilder = new CollectionSchemaBuilder( + milvusEntity.getCollectionName(), client + ); + schemaBuilder.addField(milvusEntity.getMilvusFields().toArray(new AddFieldReq[0])); + List indexParams = milvusEntity.getIndexParams(); + log.info("-------create schema---------"); + schemaBuilder.createSchema(); + if (indexParams != null && !indexParams.isEmpty()) { + schemaBuilder.createIndex(indexParams); + log.info("-------create index---------"); + } + } + private void loadStatus(String collectionName){ + GetLoadStateReq getLoadStateReq = GetLoadStateReq.builder() + .collectionName(collectionName) + .build(); + Boolean resp = client.getLoadState(getLoadStateReq); + log.info("LoadState-->{}", resp); + if(!resp){ + LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder() + .collectionName(collectionName) + .build(); + client.loadCollection(loadCollectionReq); + } + } + +} \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusProperties.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusProperties.java index 912e51e..42d7a19 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusProperties.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/config/MilvusProperties.java @@ -3,6 +3,9 @@ package io.github.javpower.milvus.plus.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; + +import java.util.List; + /** * @author xgc **/ @@ -13,4 +16,5 @@ public class MilvusProperties { private boolean enable; private String uri; private String token; + private List packages; } \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java index 45cf008..f98e591 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java @@ -1,11 +1,12 @@ package io.github.javpower.milvus.plus.converter; +import io.github.javpower.milvus.plus.annotation.ExtraParam; import io.github.javpower.milvus.plus.annotation.MilvusCollection; import io.github.javpower.milvus.plus.annotation.MilvusField; import io.github.javpower.milvus.plus.annotation.MilvusIndex; -import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.cache.CollectionToPrimaryCache; +import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.cache.MilvusCache; import io.github.javpower.milvus.plus.cache.PropertyCache; import io.github.javpower.milvus.plus.model.MilvusEntity; @@ -15,7 +16,10 @@ import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; + /** * @author xgc **/ @@ -41,18 +45,26 @@ public class MilvusEntityConverter { if (fieldAnnotation.isPrimaryKey()) { collectionToPrimaryCache.collectionToPrimary.put(collectionName,fieldName); } - AddFieldReq milvusField = AddFieldReq.builder() + AddFieldReq.AddFieldReqBuilder builder = AddFieldReq.builder() .fieldName(fieldName) .dataType(fieldAnnotation.dataType()) .isPrimaryKey(fieldAnnotation.isPrimaryKey()) - .autoID(fieldAnnotation.autoID()) - .description(StringUtils.isNotEmpty(fieldAnnotation.description()) ? fieldAnnotation.description() : null) - .dimension(fieldAnnotation.dimension()) - .maxLength(fieldAnnotation.maxLength() > 0 ? fieldAnnotation.maxLength() : null) .isPartitionKey(fieldAnnotation.isPartitionKey()) .elementType(fieldAnnotation.elementType()) - .maxCapacity(fieldAnnotation.maxCapacity() > 0 ? fieldAnnotation.maxCapacity() : null) - .build(); + .autoID(fieldAnnotation.autoID()); + if(StringUtils.isNotEmpty(fieldAnnotation.description())){ + builder.description(fieldAnnotation.description()); + } + if(fieldAnnotation.dimension()>0){ + builder.dimension(fieldAnnotation.dimension()); + } + if(fieldAnnotation.maxLength() > 0){ + builder.maxLength(fieldAnnotation.maxLength()); + } + if(fieldAnnotation.maxCapacity() > 0){ + builder.maxCapacity(fieldAnnotation.maxCapacity()); + } + AddFieldReq milvusField = builder.build(); milvusFields.add(milvusField); // 构建IndexParam对象 IndexParam indexParam = createIndexParam(field,fieldName); @@ -79,10 +91,17 @@ public class MilvusEntityConverter { if (fieldAnnotation == null) { return null; } + Map map=new HashMap<>(); + ExtraParam[] extraParams = fieldAnnotation.extraParams(); + for (ExtraParam extraParam : extraParams) { + map.put(extraParam.key(),extraParam.value()); + } return IndexParam.builder() - .fieldName(fieldAnnotation.indexName().isEmpty() ? fieldName : fieldAnnotation.indexName()) + .indexName(fieldAnnotation.indexName().isEmpty() ? fieldName : fieldAnnotation.indexName()) + .fieldName(fieldName) .indexType(fieldAnnotation.indexType()) .metricType(fieldAnnotation.metricType()) // 默认使用L2距离,根据需要调整 + .extraParams(map) .build(); } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java index 5df87de..65a1ff4 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java @@ -1,6 +1,9 @@ package io.github.javpower.milvus.plus.converter; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.javpower.milvus.plus.cache.ConversionCache; +import io.github.javpower.milvus.plus.cache.MilvusCache; +import io.github.javpower.milvus.plus.cache.PropertyCache; import io.github.javpower.milvus.plus.model.vo.MilvusResp; import io.github.javpower.milvus.plus.model.vo.MilvusResult; import io.github.javpower.milvus.plus.model.vo.MilvusResultVo; @@ -9,6 +12,7 @@ import io.milvus.v2.service.vector.response.QueryResp; import io.milvus.v2.service.vector.response.SearchResp; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -21,6 +25,8 @@ public class SearchRespConverter { private static final ObjectMapper objectMapper = new ObjectMapper(); public static MilvusResp> convertSearchRespToMilvusResp(SearchResp searchResp, Class entityType) { + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); + PropertyCache propertyCache = conversionCache.getPropertyCache(); List> searchResults = searchResp.getSearchResults(); // 将searchResults中的每个SearchResult转换为MilvusResult List>> milvusResults = searchResults.stream() @@ -28,7 +34,13 @@ public class SearchRespConverter { .map(searchResult -> { try { // 使用ObjectMapper将Map转换为Java实体类T - T entity = objectMapper.convertValue(searchResult.getEntity(), entityType); + Map entityMap = searchResult.getEntity(); + Map entityMap2=new HashMap<>(); + for (Map.Entry entry : entityMap.entrySet()) { + String key = propertyCache.findKeyByValue(entry.getKey()); + entityMap2.put(key,entry.getValue()); + } + T entity = objectMapper.convertValue(entityMap2, entityType); MilvusResult tMilvusResult = new MilvusResult<>(); tMilvusResult.setId(searchResult.getId()); tMilvusResult.setDistance(searchResult.getDistance()); @@ -55,13 +67,20 @@ public class SearchRespConverter { public static MilvusResp> convertGetRespToMilvusResp(GetResp getResp, Class entityType) { // 解析GetResp中的查询结果 + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); + PropertyCache propertyCache = conversionCache.getPropertyCache(); List getResults = getResp.getResults; List entities = new ArrayList<>(); // 遍历每个查询结果,并将它们映射到Java实体类T的实例 for (QueryResp.QueryResult queryResult : getResults) { Map entityMap = queryResult.getEntity(); + Map entityMap2=new HashMap<>(); + for (Map.Entry entry : entityMap.entrySet()) { + String key = propertyCache.findKeyByValue(entry.getKey()); + entityMap2.put(key,entry.getValue()); + } // 假设有一个方法可以从Map映射到实体类T,这个方法需要自定义实现 - T entity = objectMapper.convertValue(entityMap, entityType); + T entity = objectMapper.convertValue(entityMap2, entityType); entities.add(entity); } // 创建MilvusResp对象,并将实体列表作为其数据部分 diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaDeleteWrapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaDeleteWrapper.java index 6d7f4f0..285ac87 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaDeleteWrapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaDeleteWrapper.java @@ -4,8 +4,8 @@ import com.alibaba.fastjson.JSON; import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.core.FieldFunction; import io.github.javpower.milvus.plus.model.vo.MilvusResp; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.service.vector.request.DeleteReq; import io.milvus.v2.service.vector.response.DeleteResp; import lombok.Data; @@ -25,10 +25,10 @@ public class LambdaDeleteWrapper extends AbstractChainWrapper implements private ConversionCache conversionCache; private Class entityType; private String collectionName; - private MilvusClient client; + private MilvusClientV2 client; private List ids=new ArrayList<>(); - public LambdaDeleteWrapper(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public LambdaDeleteWrapper(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { this.collectionName = collectionName; this.client = client; this.conversionCache=conversionCache; @@ -353,7 +353,7 @@ public class LambdaDeleteWrapper extends AbstractChainWrapper implements public MilvusResp remove() throws MilvusException { DeleteReq deleteReq = build(); log.info("build remove param-->{}", JSON.toJSONString(deleteReq)); - DeleteResp delete = client.client.delete(deleteReq); + DeleteResp delete = client.delete(deleteReq); MilvusResp resp=new MilvusResp(); resp.setData(delete); resp.setSuccess(true); @@ -365,7 +365,7 @@ public class LambdaDeleteWrapper extends AbstractChainWrapper implements } @Override - public void init(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public void init(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { setClient(client); setCollectionName(collectionName); setEntityType(entityType); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaInsertWrapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaInsertWrapper.java index 71efe04..0560679 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaInsertWrapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaInsertWrapper.java @@ -6,8 +6,8 @@ import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.cache.PropertyCache; import io.github.javpower.milvus.plus.core.FieldFunction; import io.github.javpower.milvus.plus.model.vo.MilvusResp; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.service.vector.request.InsertReq; import io.milvus.v2.service.vector.response.InsertResp; import lombok.Data; @@ -27,9 +27,9 @@ public class LambdaInsertWrapper extends AbstractChainWrapper implements private ConversionCache conversionCache; private Class entityType; private String collectionName; - private MilvusClient client; + private MilvusClientV2 client; private JSONObject entity=new JSONObject(); - public LambdaInsertWrapper(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public LambdaInsertWrapper(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { this.collectionName = collectionName; this.client = client; this.conversionCache=conversionCache; @@ -66,7 +66,7 @@ public class LambdaInsertWrapper extends AbstractChainWrapper implements .collectionName(collectionName) .data(jsonObjects) .build(); - InsertResp insert = client.client.insert(insertReq); + InsertResp insert = client.insert(insertReq); MilvusResp resp=new MilvusResp(); resp.setData(insert); resp.setSuccess(true); @@ -92,7 +92,7 @@ public class LambdaInsertWrapper extends AbstractChainWrapper implements } @Override - public void init(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public void init(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { setClient(client); setCollectionName(collectionName); setEntityType(entityType); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaSearchWrapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaSearchWrapper.java index 036e130..1d58d42 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaSearchWrapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaSearchWrapper.java @@ -6,8 +6,8 @@ import io.github.javpower.milvus.plus.converter.SearchRespConverter; import io.github.javpower.milvus.plus.core.FieldFunction; import io.github.javpower.milvus.plus.model.vo.MilvusResp; import io.github.javpower.milvus.plus.model.vo.MilvusResultVo; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.common.ConsistencyLevel; import io.milvus.v2.service.vector.request.GetReq; import io.milvus.v2.service.vector.request.SearchReq; @@ -40,9 +40,9 @@ public class LambdaSearchWrapper extends AbstractChainWrapper implements private long guaranteeTimestamp; private ConsistencyLevel consistencyLevel; private boolean ignoreGrowing; - private MilvusClient client; + private MilvusClientV2 client; - public LambdaSearchWrapper(String collectionName, MilvusClient client,ConversionCache conversionCache,Class entityType) { + public LambdaSearchWrapper(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { this.collectionName = collectionName; this.client = client; this.conversionCache=conversionCache; @@ -391,7 +391,7 @@ public class LambdaSearchWrapper extends AbstractChainWrapper implements public MilvusResp> query() throws MilvusException { SearchReq searchReq = build(); log.info("build query param-->{}", JSON.toJSONString(searchReq)); - SearchResp search = client.client.search(searchReq); + SearchResp search = client.search(searchReq); MilvusResp> tMilvusResp = SearchRespConverter.convertSearchRespToMilvusResp(search, entityType); return tMilvusResp; } @@ -400,14 +400,13 @@ public class LambdaSearchWrapper extends AbstractChainWrapper implements .collectionName(collectionName) .ids(Arrays.asList(ids)) .build(); - GetResp getResp = client.client.get(getReq); + GetResp getResp = client.get(getReq); MilvusResp> tMilvusResp = SearchRespConverter.convertGetRespToMilvusResp(getResp, entityType); return tMilvusResp; } - @Override - public void init(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public void init(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { setClient(client); setCollectionName(collectionName); setEntityType(entityType); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaUpdateWrapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaUpdateWrapper.java index b866b38..1d42a43 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaUpdateWrapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/LambdaUpdateWrapper.java @@ -7,8 +7,8 @@ import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.cache.PropertyCache; import io.github.javpower.milvus.plus.core.FieldFunction; import io.github.javpower.milvus.plus.model.vo.MilvusResp; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.milvus.exception.MilvusException; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.service.vector.request.SearchReq; import io.milvus.v2.service.vector.request.UpsertReq; import io.milvus.v2.service.vector.response.SearchResp; @@ -29,9 +29,9 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements private ConversionCache conversionCache; private Class entityType; private String collectionName; - private MilvusClient client; + private MilvusClientV2 client; - public LambdaUpdateWrapper(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public LambdaUpdateWrapper(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { this.collectionName = collectionName; this.client = client; this.conversionCache=conversionCache; @@ -327,7 +327,7 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements if (filterStr != null && !filterStr.isEmpty()) { SearchReq.SearchReqBuilder builder = SearchReq.builder() .collectionName(collectionName).filter(filterStr); - SearchResp search = client.client.search(builder.build()); + SearchResp search = client.search(builder.build()); return search; }else { return null; @@ -383,7 +383,7 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements .collectionName(collectionName) .data(jsonObjects) .build(); - UpsertResp upsert = client.client.upsert(upsertReq); + UpsertResp upsert = client.upsert(upsertReq); MilvusResp resp=new MilvusResp(); resp.setData(upsert); resp.setSuccess(true); @@ -414,7 +414,7 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements } @Override - public void init(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType) { + public void init(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType) { setClient(client); setCollectionName(collectionName); setEntityType(entityType); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/Wrapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/Wrapper.java index bd2f1c7..4003fff 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/Wrapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/conditions/Wrapper.java @@ -1,7 +1,7 @@ package io.github.javpower.milvus.plus.core.conditions; import io.github.javpower.milvus.plus.cache.ConversionCache; -import io.github.javpower.milvus.plus.service.MilvusClient; +import io.milvus.v2.client.MilvusClientV2; /** * 通用构建器接口 @@ -9,6 +9,6 @@ import io.github.javpower.milvus.plus.service.MilvusClient; * @param 实体类型 */ public interface Wrapper { - void init(String collectionName, MilvusClient client, ConversionCache conversionCache, Class entityType); + void init(String collectionName, MilvusClientV2 client, ConversionCache conversionCache, Class entityType); W wrapper(); } \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java index 1cf6f70..49c1f64 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java @@ -5,8 +5,8 @@ import io.github.javpower.milvus.plus.cache.ConversionCache; import io.github.javpower.milvus.plus.cache.MilvusCache; import io.github.javpower.milvus.plus.core.conditions.*; import io.github.javpower.milvus.plus.model.vo.MilvusResp; -import io.github.javpower.milvus.plus.service.MilvusClient; import io.github.javpower.milvus.plus.util.SpringUtils; +import io.milvus.v2.client.MilvusClientV2; import io.milvus.v2.service.vector.response.DeleteResp; import io.milvus.v2.service.vector.response.InsertResp; import io.milvus.v2.service.vector.response.UpsertResp; @@ -91,7 +91,7 @@ public class MilvusMapper { ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); String collectionName = conversionCache == null ? null : conversionCache.getCollectionName(); // 使用SpringUtil获取MilvusClient实例 - MilvusClient client = SpringUtils.getBean(MilvusClient.class); + MilvusClientV2 client = SpringUtils.getBean(MilvusClientV2.class); // 初始化构建器实例 wrapper.init(collectionName, client, conversionCache, entityType); return wrapper.wrapper(); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusClient.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusClient.java deleted file mode 100644 index 478860a..0000000 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusClient.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.javpower.milvus.plus.service; - - -import io.milvus.v2.client.MilvusClientV2; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -/** - * @author xgc - **/ -@Service -public class MilvusClient implements AutoCloseable { - @Autowired(required = false) - public MilvusClientV2 client; - - @Override - public void close() throws InterruptedException { - client.close(10); - } -} \ No newline at end of file diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusCollectionService.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusCollectionService.java deleted file mode 100644 index 8562a6d..0000000 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/service/MilvusCollectionService.java +++ /dev/null @@ -1,77 +0,0 @@ -package io.github.javpower.milvus.plus.service; - - -import io.github.javpower.milvus.plus.builder.CollectionSchemaBuilder; -import io.github.javpower.milvus.plus.converter.MilvusEntityConverter; -import io.github.javpower.milvus.plus.model.MilvusEntity; -import io.milvus.exception.MilvusException; -import io.milvus.v2.common.IndexParam; -import io.milvus.v2.service.collection.request.AddFieldReq; -import io.milvus.v2.service.collection.request.DescribeCollectionReq; -import io.milvus.v2.service.collection.request.DropCollectionReq; -import io.milvus.v2.service.collection.request.HasCollectionReq; -import io.milvus.v2.service.collection.response.DescribeCollectionResp; -import org.springframework.stereotype.Service; - -import java.util.HashSet; -import java.util.List; -import java.util.stream.Collectors; -/** - * @author xgc - **/ -@Service -public class MilvusCollectionService { - private final MilvusClient milvusClient; - - public MilvusCollectionService(MilvusClient milvusClient) { - this.milvusClient = milvusClient; - } - - public void performBusinessLogic(List> annotatedClasses) { - for (Class milvusClass : annotatedClasses) { - MilvusEntity milvusEntity = MilvusEntityConverter.convert(milvusClass); - try { - String collectionName = milvusEntity.getCollectionName(); - // 检查集合是否存在 - boolean collectionExists = milvusClient.client.hasCollection( - HasCollectionReq.builder().collectionName(collectionName).build() - ); - if (collectionExists) { - // 获取集合的详细信息 - DescribeCollectionResp collectionInfo = milvusClient.client.describeCollection( - DescribeCollectionReq.builder().collectionName(collectionName).build() - ); - // 检查字段是否一致,这里需要实现字段比较逻辑 - List existingFieldNames = collectionInfo.getFieldNames(); - List requiredFields = milvusEntity.getMilvusFields(); - List requiredFieldNames = requiredFields.stream().map(AddFieldReq::getFieldName).collect(Collectors.toList()); - if (!new HashSet<>(existingFieldNames).containsAll(requiredFieldNames) || !new HashSet<>(requiredFieldNames).containsAll(existingFieldNames)) { - // 字段不一致,删除并重新创建集合 - milvusClient.client.dropCollection( - DropCollectionReq.builder().collectionName(collectionName).build() - ); - // 创建新集合 - create(milvusEntity); - } - } else { - // 创建新集合 - create(milvusEntity); - } - } catch (MilvusException e) { - throw new RuntimeException("Error handling Milvus collection", e); - } - } - } - private void create(MilvusEntity milvusEntity){ - // 创建新集合 - CollectionSchemaBuilder schemaBuilder = new CollectionSchemaBuilder( - milvusEntity.getCollectionName(), milvusClient - ); - schemaBuilder.addField(milvusEntity.getMilvusFields().toArray(new AddFieldReq[0])); - List indexParams = milvusEntity.getIndexParams(); - schemaBuilder.createSchema(); - if (indexParams != null && !indexParams.isEmpty()) { - schemaBuilder.createIndex(indexParams); - } - } -} \ No newline at end of file