Browse Source

add: 向量数据库根据条件删除相关数据api

zhongwei
haungxing 2 months ago
parent
commit
f1c76ce1cb
  1. 8
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/controller/WeaviateController.java
  2. 51
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java

8
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/controller/WeaviateController.java

@ -9,6 +9,7 @@ import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author: huangxing * @Author: huangxing
@ -44,4 +45,11 @@ public class WeaviateController {
public R<Object> query(@RequestBody WeaviateQueryDTO query) { public R<Object> query(@RequestBody WeaviateQueryDTO query) {
return R.data(weaviateService.query(query.getResultFields(),query.getClassName(),query.getQuery())); return R.data(weaviateService.query(query.getResultFields(),query.getClassName(),query.getQuery()));
} }
@DeleteMapping("/deleteCondition")
public R<Boolean> deleteCondition(@RequestBody Map<String,Object> request) {
String className = (String) request.get("className");
Map<String,String> condition = (Map<String, String>) request.get("condition");
return R.status(weaviateService.deleteCondition(className,condition));
}
} }

51
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java

@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SerializerFeature;
@ -11,6 +12,7 @@ import com.google.common.collect.Lists;
import com.hnac.gglm.bigmodel.BigModelConstants; import com.hnac.gglm.bigmodel.BigModelConstants;
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi;
import com.hnac.gglm.bigmodel.utils.RequestClientUtil; import com.hnac.gglm.bigmodel.utils.RequestClientUtil;
import com.hnac.hzims.fdp.constants.ScheduledConstant;
import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.exception.HzServiceException;
import io.weaviate.client.Config; import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient; import io.weaviate.client.WeaviateAuthClient;
@ -100,7 +102,7 @@ public class WeaviateService {
List<List> entitiesList = splitList(entities, 1000); List<List> entitiesList = splitList(entities, 1000);
int total = 0; int total = 0;
for (List entityList : entitiesList) { for (List entityList : entitiesList) {
Integer insert = this.insert(entityList, className, attrsMap, params); Integer insert = this.insert(entityList, attrsMap, params);
total += insert; total += insert;
} }
// 查询weaviate 中该表的数据量 // 查询weaviate 中该表的数据量
@ -108,6 +110,36 @@ public class WeaviateService {
} }
/** /**
* 根据条件删除数据
* @param className 表名
* @param condition 查询条件
* @return 删除结果
*/
public Boolean deleteCondition(String className,Map<String,String> condition) {
// 查询到相关数据
Object query = this.query(null, className, condition);
if(Func.isEmpty(query)) {
throw new HzServiceException("暂无数据,删除失败!");
}
JSONObject queryJson = JSONObject.parseObject(JSON.toJSONString(query));
JSONArray data = Optional.ofNullable(queryJson).map(json -> json.getJSONObject("Get"))
.map(json -> json.getJSONArray(className)).orElse(null);
if(Func.isNotEmpty(data)) {
List<String> ids = data.stream().map(item -> {
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(item));
return Optional.ofNullable(jsonObject)
.map(json -> json.getJSONObject("_additional"))
.map(json -> json.getString("id"))
.orElse("");
}).filter(Func::isNotEmpty).collect(Collectors.toList());
if(Func.isNotEmpty(ids)) {
this.delete(ids.stream().collect(Collectors.joining(",")), className);
}
}
return true;
}
/**
* 将list按size截断为多个list * 将list按size截断为多个list
* @param list 待截断的list * @param list 待截断的list
* @param size 截断大小 * @param size 截断大小
@ -122,14 +154,21 @@ public class WeaviateService {
return parts; return parts;
} }
private Integer insert(List entities,String className, Map<String,String> attrsMap, Map<String,Object> params) { /**
* 批量插入数据
* @param entities 待插入的数据
* @param attrsMap 向量計算Map
* @param params 向量計算参数
* @return 插入数量
*/
private Integer insert(List entities, Map<String,String> attrsMap, Map<String,Object> params) {
List<Map<String, Object>> data = new ArrayList<>(); List<Map<String, Object>> data = new ArrayList<>();
entities.forEach(entity -> { entities.forEach(entity -> {
// 将entity转换为Map<String,String> // 将entity转换为Map<String,String>
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue)); JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue));
Map<String,String> map = new HashMap<>(); Map<String,Object> map = new HashMap<>();
data.add(this.getVectorData(map,attrsMap)); data.add(this.getVectorData(map,attrsMap));
jsonObject.forEach((k,v) -> map.put(k,jsonObject.getString(k))); jsonObject.forEach((k,v) -> map.put(k,jsonObject.get(k)));
}); });
params.put("data",data); params.put("data",data);
String url = gglmUrl + invokeApi.getInsertVectors(); String url = gglmUrl + invokeApi.getInsertVectors();
@ -138,14 +177,14 @@ public class WeaviateService {
return (Integer) stringIntegerMap.get("total"); return (Integer) stringIntegerMap.get("total");
} }
private Map<String,Object> getVectorData(Map<String,String> entity,Map<String,String> attrsMap) { private Map<String,Object> getVectorData(Map<String,Object> entity,Map<String,String> attrsMap) {
Map<String,Object> result = new HashMap<>(2); Map<String,Object> result = new HashMap<>(2);
result.put("object", entity); result.put("object", entity);
List<Map<String,String>> vectors = new ArrayList<>(); List<Map<String,String>> vectors = new ArrayList<>();
attrsMap.forEach((k,fields) -> { attrsMap.forEach((k,fields) -> {
Map<String,String> vector = new HashMap<>(); Map<String,String> vector = new HashMap<>();
vector.put("key",k); vector.put("key",k);
String value = Func.toStrList(",", fields).stream().map(field -> entity.get(field)).collect(Collectors.joining(" ")); String value = Func.toStrList(",", fields).stream().map(field -> entity.get(field)).map(String::valueOf).collect(Collectors.joining(" "));
vector.put("content", value); vector.put("content", value);
vectors.add(vector); vectors.add(vector);
}); });

Loading…
Cancel
Save