From 610fdf1688e09a1e898e20c508bb1dd8d3530f0a Mon Sep 17 00:00:00 2001 From: xgc Date: Thu, 6 Jun 2024 17:56:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=9B=B4=E6=96=B0=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E9=9C=80=E8=A6=81=E5=85=A8=E9=83=A8=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/conditions/LambdaInsertWrapper.java | 8 ++ .../core/conditions/LambdaUpdateWrapper.java | 103 ++++++++++++------ .../service/AbstractMilvusClientBuilder.java | 2 +- 3 files changed, 76 insertions(+), 37 deletions(-) diff --git a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaInsertWrapper.java b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaInsertWrapper.java index 7c35ffa..83061a6 100644 --- a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaInsertWrapper.java +++ b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaInsertWrapper.java @@ -10,10 +10,13 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.dromara.milvus.plus.cache.CollectionToPrimaryCache; import org.dromara.milvus.plus.cache.ConversionCache; +import org.dromara.milvus.plus.cache.MilvusCache; import org.dromara.milvus.plus.cache.PropertyCache; import org.dromara.milvus.plus.core.FieldFunction; import org.dromara.milvus.plus.model.vo.MilvusResp; +import org.dromara.milvus.plus.util.IdWorkerUtils; import java.util.*; @@ -90,7 +93,9 @@ public class LambdaInsertWrapper extends AbstractChainWrapper implements return insert(iterator); } public MilvusResp insert(Iterator iterator) throws MilvusException { + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName()); PropertyCache propertyCache = conversionCache.getPropertyCache(); + String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName); List jsonObjects=new ArrayList<>(); while (iterator.hasNext()) { T t1 = iterator.next(); @@ -102,6 +107,9 @@ public class LambdaInsertWrapper extends AbstractChainWrapper implements String tk = propertyCache.functionToPropertyMap.get(key); jsonObject.put(tk,value); } + if(conversionCache.isAutoID()){ + jsonObject.put(pk, IdWorkerUtils.nextId()); + } jsonObjects.add(jsonObject); } return insert(jsonObjects); diff --git a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaUpdateWrapper.java b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaUpdateWrapper.java index c74403c..84dcc77 100644 --- a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaUpdateWrapper.java +++ b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/core/conditions/LambdaUpdateWrapper.java @@ -4,9 +4,9 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; 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.QueryReq; import io.milvus.v2.service.vector.request.UpsertReq; -import io.milvus.v2.service.vector.response.SearchResp; +import io.milvus.v2.service.vector.response.QueryResp; import io.milvus.v2.service.vector.response.UpsertResp; import lombok.Data; import lombok.EqualsAndHashCode; @@ -340,12 +340,12 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements W * * @return 搜索请求对象 */ - private SearchResp build() { + private QueryResp build() { String filterStr = buildFilters(); if (filterStr != null && !filterStr.isEmpty()) { - SearchReq.SearchReqBuilder builder = SearchReq.builder() + QueryReq.QueryReqBuilder builder = QueryReq.builder() .collectionName(collectionName).filter(filterStr); - return client.search(builder.build()); + return client.query(builder.build()); } else { return null; } @@ -356,43 +356,74 @@ public class LambdaUpdateWrapper extends AbstractChainWrapper implements W * * @return 更新响应对象 */ - public MilvusResp update(T t) throws MilvusException { - List jsonObjects = new ArrayList<>(); - SearchResp searchResp = build(); - List ids = new ArrayList<>(); - if (searchResp != null) { - for (List searchResult : searchResp.getSearchResults()) { - for (SearchResp.SearchResult result : searchResult) { - ids.add(result.getId()); + public MilvusResp update(T entity) throws MilvusException { + // 获取主键字段 + String primaryKeyField = CollectionToPrimaryCache.collectionToPrimary.get(collectionName); + // 将实体转换为属性映射 + Map propertiesMap = getPropertiesMap(entity); + PropertyCache propertyCache = conversionCache.getPropertyCache(); + // 初始化主键标识和主键值 + boolean hasPrimaryKey = false; + Object primaryKeyValue = null; + + // 准备更新的数据列表 + List updateDataList = new ArrayList<>(); + + // 构建单个更新对象 + JSONObject updateObject = new JSONObject(); + for (Map.Entry entry : propertiesMap.entrySet()) { + String field = entry.getKey(); + Object value = entry.getValue(); + String tableNameColumn = propertyCache.functionToPropertyMap.get(field); + // 检查是否为主键字段 + if (primaryKeyField.equals(tableNameColumn)) { + hasPrimaryKey = true; + primaryKeyValue = value; + } + // 添加到更新对象 + updateObject.put(tableNameColumn, value); + } + + // 检查是否需要构建查询条件 + boolean needBuildQuery = !hasPrimaryKey; + if (hasPrimaryKey) { + for (Map.Entry property : propertyCache.functionToPropertyMap.entrySet()) { + if (updateObject.get(property.getValue()) == null) { + needBuildQuery = true; + eq(primaryKeyField,primaryKeyValue); + break; } } } - Map propertiesMap = getPropertiesMap(t); - PropertyCache propertyCache = conversionCache.getPropertyCache(); - String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName); - boolean havePk = false; - JSONObject jsonObject = new JSONObject(); - for (Map.Entry entry : propertiesMap.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue(); - String tk = propertyCache.functionToPropertyMap.get(key); - if (pk.equals(tk)) { - havePk = true; + + // 如果需要构建查询条件,则执行查询并准备更新数据 + if (needBuildQuery) { + QueryResp queryResp = build(); + if (queryResp != null) { + for (QueryResp.QueryResult result : queryResp.getQueryResults()) { + Map existingEntity = result.getEntity(); + JSONObject existingData = new JSONObject(); + + for (Map.Entry existingEntry : existingEntity.entrySet()) { + String existingField = existingEntry.getKey(); + Object existingValue = existingEntry.getValue(); + Object updateValue = updateObject.get(existingField); + existingData.put(existingField, updateValue != null ? updateValue : existingValue); + } + + updateDataList.add(existingData); + } } - jsonObject.put(tk, value); - } - if (!havePk && ids.isEmpty()) { - throw new MilvusException("not find primary key", 400); - } - if (havePk) { - jsonObjects.add(jsonObject); } else { - for (Object id : ids) { - jsonObject.put(pk, id); - jsonObjects.add(jsonObject); - } + updateDataList.add(updateObject); } - return update(jsonObjects); + + // 检查是否有数据需要更新 + if (updateDataList.isEmpty()) { + return new MilvusResp<>(); + } + // 执行更新操作 + return update(updateDataList); } private MilvusResp update(List jsonObjects) { diff --git a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/service/AbstractMilvusClientBuilder.java b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/service/AbstractMilvusClientBuilder.java index 1b72179..2ba5eb4 100644 --- a/milvus-plus-core/src/main/java/org/dromara/milvus/plus/service/AbstractMilvusClientBuilder.java +++ b/milvus-plus-core/src/main/java/org/dromara/milvus/plus/service/AbstractMilvusClientBuilder.java @@ -121,7 +121,7 @@ public abstract class AbstractMilvusClientBuilder implements MilvusClientBuilder } private void aliasProcess(MilvusEntity milvusEntity) { - if (StringUtils.isBlank(milvusEntity.getCollectionName()) || milvusEntity.getAlias().isEmpty()) { + if (StringUtils.isBlank(milvusEntity.getCollectionName()) || milvusEntity.getAlias()==null|| milvusEntity.getAlias().isEmpty()) { return; } ListAliasResp listAliasResp = listAliases(milvusEntity);