mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-07 01:18:23 +08:00
解决更新必须需要全部字段数据的bug
This commit is contained in:
parent
a7454652d2
commit
610fdf1688
@ -10,10 +10,13 @@ import lombok.Data;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
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.ConversionCache;
|
||||||
|
import org.dromara.milvus.plus.cache.MilvusCache;
|
||||||
import org.dromara.milvus.plus.cache.PropertyCache;
|
import org.dromara.milvus.plus.cache.PropertyCache;
|
||||||
import org.dromara.milvus.plus.core.FieldFunction;
|
import org.dromara.milvus.plus.core.FieldFunction;
|
||||||
import org.dromara.milvus.plus.model.vo.MilvusResp;
|
import org.dromara.milvus.plus.model.vo.MilvusResp;
|
||||||
|
import org.dromara.milvus.plus.util.IdWorkerUtils;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -90,7 +93,9 @@ public class LambdaInsertWrapper<T> extends AbstractChainWrapper<T> implements
|
|||||||
return insert(iterator);
|
return insert(iterator);
|
||||||
}
|
}
|
||||||
public MilvusResp<InsertResp> insert(Iterator<T> iterator) throws MilvusException {
|
public MilvusResp<InsertResp> insert(Iterator<T> iterator) throws MilvusException {
|
||||||
|
ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName());
|
||||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||||
|
String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);
|
||||||
List<JSONObject> jsonObjects=new ArrayList<>();
|
List<JSONObject> jsonObjects=new ArrayList<>();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
T t1 = iterator.next();
|
T t1 = iterator.next();
|
||||||
@ -102,6 +107,9 @@ public class LambdaInsertWrapper<T> extends AbstractChainWrapper<T> implements
|
|||||||
String tk = propertyCache.functionToPropertyMap.get(key);
|
String tk = propertyCache.functionToPropertyMap.get(key);
|
||||||
jsonObject.put(tk,value);
|
jsonObject.put(tk,value);
|
||||||
}
|
}
|
||||||
|
if(conversionCache.isAutoID()){
|
||||||
|
jsonObject.put(pk, IdWorkerUtils.nextId());
|
||||||
|
}
|
||||||
jsonObjects.add(jsonObject);
|
jsonObjects.add(jsonObject);
|
||||||
}
|
}
|
||||||
return insert(jsonObjects);
|
return insert(jsonObjects);
|
||||||
|
|||||||
@ -4,9 +4,9 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import io.milvus.exception.MilvusException;
|
import io.milvus.exception.MilvusException;
|
||||||
import io.milvus.v2.client.MilvusClientV2;
|
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.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 io.milvus.v2.service.vector.response.UpsertResp;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@ -340,12 +340,12 @@ public class LambdaUpdateWrapper<T> extends AbstractChainWrapper<T> implements W
|
|||||||
*
|
*
|
||||||
* @return 搜索请求对象
|
* @return 搜索请求对象
|
||||||
*/
|
*/
|
||||||
private SearchResp build() {
|
private QueryResp build() {
|
||||||
String filterStr = buildFilters();
|
String filterStr = buildFilters();
|
||||||
if (filterStr != null && !filterStr.isEmpty()) {
|
if (filterStr != null && !filterStr.isEmpty()) {
|
||||||
SearchReq.SearchReqBuilder<?, ?> builder = SearchReq.builder()
|
QueryReq.QueryReqBuilder<?, ?> builder = QueryReq.builder()
|
||||||
.collectionName(collectionName).filter(filterStr);
|
.collectionName(collectionName).filter(filterStr);
|
||||||
return client.search(builder.build());
|
return client.query(builder.build());
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -356,43 +356,74 @@ public class LambdaUpdateWrapper<T> extends AbstractChainWrapper<T> implements W
|
|||||||
*
|
*
|
||||||
* @return 更新响应对象
|
* @return 更新响应对象
|
||||||
*/
|
*/
|
||||||
public MilvusResp<UpsertResp> update(T t) throws MilvusException {
|
public MilvusResp<UpsertResp> update(T entity) throws MilvusException {
|
||||||
List<JSONObject> jsonObjects = new ArrayList<>();
|
// 获取主键字段
|
||||||
SearchResp searchResp = build();
|
String primaryKeyField = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);
|
||||||
List<Object> ids = new ArrayList<>();
|
// 将实体转换为属性映射
|
||||||
if (searchResp != null) {
|
Map<String, Object> propertiesMap = getPropertiesMap(entity);
|
||||||
for (List<SearchResp.SearchResult> searchResult : searchResp.getSearchResults()) {
|
|
||||||
for (SearchResp.SearchResult result : searchResult) {
|
|
||||||
ids.add(result.getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Map<String, Object> propertiesMap = getPropertiesMap(t);
|
|
||||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||||
String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);
|
// 初始化主键标识和主键值
|
||||||
boolean havePk = false;
|
boolean hasPrimaryKey = false;
|
||||||
JSONObject jsonObject = new JSONObject();
|
Object primaryKeyValue = null;
|
||||||
|
|
||||||
|
// 准备更新的数据列表
|
||||||
|
List<JSONObject> updateDataList = new ArrayList<>();
|
||||||
|
|
||||||
|
// 构建单个更新对象
|
||||||
|
JSONObject updateObject = new JSONObject();
|
||||||
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
||||||
String key = entry.getKey();
|
String field = entry.getKey();
|
||||||
Object value = entry.getValue();
|
Object value = entry.getValue();
|
||||||
String tk = propertyCache.functionToPropertyMap.get(key);
|
String tableNameColumn = propertyCache.functionToPropertyMap.get(field);
|
||||||
if (pk.equals(tk)) {
|
// 检查是否为主键字段
|
||||||
havePk = true;
|
if (primaryKeyField.equals(tableNameColumn)) {
|
||||||
|
hasPrimaryKey = true;
|
||||||
|
primaryKeyValue = value;
|
||||||
}
|
}
|
||||||
jsonObject.put(tk, value);
|
// 添加到更新对象
|
||||||
|
updateObject.put(tableNameColumn, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否需要构建查询条件
|
||||||
|
boolean needBuildQuery = !hasPrimaryKey;
|
||||||
|
if (hasPrimaryKey) {
|
||||||
|
for (Map.Entry<String, String> property : propertyCache.functionToPropertyMap.entrySet()) {
|
||||||
|
if (updateObject.get(property.getValue()) == null) {
|
||||||
|
needBuildQuery = true;
|
||||||
|
eq(primaryKeyField,primaryKeyValue);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果需要构建查询条件,则执行查询并准备更新数据
|
||||||
|
if (needBuildQuery) {
|
||||||
|
QueryResp queryResp = build();
|
||||||
|
if (queryResp != null) {
|
||||||
|
for (QueryResp.QueryResult result : queryResp.getQueryResults()) {
|
||||||
|
Map<String, Object> existingEntity = result.getEntity();
|
||||||
|
JSONObject existingData = new JSONObject();
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> 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);
|
||||||
}
|
}
|
||||||
if (!havePk && ids.isEmpty()) {
|
|
||||||
throw new MilvusException("not find primary key", 400);
|
|
||||||
}
|
}
|
||||||
if (havePk) {
|
|
||||||
jsonObjects.add(jsonObject);
|
|
||||||
} else {
|
} else {
|
||||||
for (Object id : ids) {
|
updateDataList.add(updateObject);
|
||||||
jsonObject.put(pk, id);
|
|
||||||
jsonObjects.add(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否有数据需要更新
|
||||||
|
if (updateDataList.isEmpty()) {
|
||||||
|
return new MilvusResp<>();
|
||||||
}
|
}
|
||||||
return update(jsonObjects);
|
// 执行更新操作
|
||||||
|
return update(updateDataList);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MilvusResp<UpsertResp> update(List<JSONObject> jsonObjects) {
|
private MilvusResp<UpsertResp> update(List<JSONObject> jsonObjects) {
|
||||||
|
|||||||
@ -121,7 +121,7 @@ public abstract class AbstractMilvusClientBuilder implements MilvusClientBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void aliasProcess(MilvusEntity milvusEntity) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
ListAliasResp listAliasResp = listAliases(milvusEntity);
|
ListAliasResp listAliasResp = listAliases(milvusEntity);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user