根据caseId查节点类型,关系类型集合接口

topo_dev
DESKTOP-DDTUS3E\yaxin 6 months ago
parent 0633948570
commit d060dc914f

@ -23,7 +23,7 @@ public class Neo4jConfig {
// 这里可以添加额外的配置,比如加密、连接池设置等
Config config = Config.builder()
// 示例:关闭加密(注意:在生产环境中应该启用加密)
.withoutEncryption()
// .withoutEncryption()
// 你可以在这里添加更多的配置选项
.build();

@ -89,6 +89,10 @@ public class Neo4jController {
public R<?> getNode(@RequestParam String picType, @RequestParam String caseId) {
return neo4jService.getNode(picType, caseId);
}
@GetMapping("/getNodeAndRelationListByCaseId")
public R<?> getNodeAndRelationListByCaseId(@RequestParam String picType, @RequestParam String caseId) {
return neo4jService.getNodeAndRelationListByCaseId(picType, caseId);
}
// @GetMapping("/test")
// public R<?> test() {

@ -35,6 +35,8 @@ public interface Neo4jService {
R<?> getNode(String picType, String caseId);
R<?> getNodeAndRelationListByCaseId(String picType, String caseId);
// R<?> test();
void deleteAbstractGraph();

@ -12,8 +12,8 @@ import com.supervision.neo4j.service.Neo4jService;
import com.supervision.neo4j.utils.Neo4jUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.*;
import org.neo4j.driver.Record;
import org.neo4j.driver.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -106,7 +106,7 @@ public class Neo4jServiceImpl implements Neo4jService {
try {
Session session = driver.session();
StringBuffer cql = new StringBuffer();
cql.append("MATCH (n) WHERE n.id = " ).append(id).append(" AND NOT (n)--() DELETE n");
cql.append("MATCH (n) WHERE n.id = ").append(id).append(" AND NOT (n)--() DELETE n");
log.info(cql.toString());
Result run = session.run(cql.toString());
while (run.hasNext()) {
@ -144,9 +144,7 @@ public class Neo4jServiceImpl implements Neo4jService {
CaseNode node = null;
try {
Session session = driver.session();
StringBuffer cql = new StringBuffer();
cql.append("MATCH (n) where id(n) = ").append(id).append(Neo4jUtils.NODE_RETURN);
Result run = session.run(cql.toString());
Result run = session.run("MATCH (n) where id(n) = " + id + Neo4jUtils.NODE_RETURN);
node = Neo4jUtils.getOneNode(run);
} catch (Exception e) {
e.printStackTrace();
@ -234,14 +232,13 @@ public class Neo4jServiceImpl implements Neo4jService {
public Rel findRelation(Rel rel) {
try {
Session session = driver.session();
StringBuffer cql = new StringBuffer();
Map<String, Object> params = new HashMap<>();
cql.append("MATCH (a)-[rel:").append(rel.getName()).append("]->(b) where id(a) = $sourceId and id(b) = $targetId")
.append(Neo4jUtils.REL_RETURN);
String cql = "MATCH (a)-[rel:" + rel.getName() + "]->(b) where id(a) = $sourceId and id(b) = $targetId" +
Neo4jUtils.REL_RETURN;
params.put("sourceId", rel.getSourceId());
params.put("targetId", rel.getTargetId());
Result run = session.run(cql.toString(), params);
Result run = session.run(cql, params);
rel = Neo4jUtils.getOneRel(run);
} catch (Exception e) {
e.printStackTrace();
@ -258,14 +255,13 @@ public class Neo4jServiceImpl implements Neo4jService {
Rel res = null;
try {
Session session = driver.session();
StringBuffer cql = new StringBuffer();
Map<String, Object> params = new HashMap<>();
cql.append("MATCH (a), (b) where id(a) = $sourceId and id(b) = $targetId CREATE(a)-[rel:").append(rel.getName())
.append("]->(b) ").append(Neo4jUtils.REL_RETURN);
String cql = "MATCH (a), (b) where id(a) = $sourceId and id(b) = $targetId CREATE(a)-[rel:" + rel.getName() +
"]->(b) " + Neo4jUtils.REL_RETURN;
params.put("sourceId", rel.getSourceId());
params.put("targetId", rel.getTargetId());
Result run = session.run(cql.toString(), params);
Result run = session.run(cql, params);
rel = Neo4jUtils.getOneRel(run);
} catch (Exception e) {
e.printStackTrace();
@ -312,7 +308,7 @@ public class Neo4jServiceImpl implements Neo4jService {
// 节点和关系合并
Map<String, NodeMapRecord> nodeRecordMap = electNodeRecord(nodes);
list = mergerWebRel(list,nodeRecordMap);
list = mergerWebRel(list, nodeRecordMap);
nodes = mergeNode(nodes, nodeRecordMap);
map.put("list", list);
@ -320,6 +316,40 @@ public class Neo4jServiceImpl implements Neo4jService {
return R.ok(map);
}
@Override
public R<?> getNodeAndRelationListByCaseId(String picType, String caseId) {
Map<String, Set<String>> map = new HashMap<>();
Set<String> nodeLabels = new HashSet<>();
Set<String> relTypes = new HashSet<>();
try (Session session = driver.session()) {
// 查询所有匹配caseId的节点及其关联关系
String query = "MATCH (n)-[r]->(m) WHERE n.caseId = $caseId AND m.caseId = $caseId RETURN labels(n) as sourceLabels, type(r) as relName, labels(m) as targetLabels";
session.executeRead(tx -> {
Result result = tx.run(query, Values.parameters("caseId", caseId));
while (result.hasNext()) {
Record record = result.next();
List<String> sourceLabels = record.get("sourceLabels").asList(Value::asString);
List<String> targetLabels = record.get("targetLabels").asList(Value::asString);
if (!sourceLabels.isEmpty()) {
nodeLabels.add(sourceLabels.get(0));
}
if (!targetLabels.isEmpty()) {
nodeLabels.add(targetLabels.get(0));
}
relTypes.add(record.get("relName").asString());
}
return null;
});
} catch (Exception e) {
log.error("查询失败", e);
}
log.info("查询到的节点类型{}个:{}", nodeLabels.size(), nodeLabels);
log.info("查询到的关系类型{}个:{}", relTypes.size(), relTypes);
map.put("nodeLabels", nodeLabels);
map.put("relTypes", relTypes);
return R.ok(map);
}
record NodeMapRecord(String name, String id, Set<String> idSet) {
}
@ -327,34 +357,37 @@ public class Neo4jServiceImpl implements Neo4jService {
/**
*
*
* @param nodes key: name ,entityName,id
* @return
*/
private Map<String, NodeMapRecord> electNodeRecord(List<Map<String, String>> nodes){
private Map<String, NodeMapRecord> electNodeRecord(List<Map<String, String>> nodes) {
Map<String, NodeMapRecord> nodeRecordMap = new HashMap<>();
for (Map<String, String> node : nodes) {
String name = node.get("name");
String id = node.get("id");
NodeMapRecord nodeMapRecord = nodeRecordMap.get(name);
if (nodeMapRecord == null){
if (nodeMapRecord == null) {
Set<String> idSet = new HashSet<>();
idSet.add(id);
nodeRecordMap.put(name, new NodeMapRecord(name, id,idSet));
}else {
nodeRecordMap.put(name, new NodeMapRecord(name, id, idSet));
} else {
nodeMapRecord.idSet.add(id);
}
}
return nodeRecordMap;
}
/**
*
*
*
* name
*
* @param nodes key: name ,entityName,id
* @param nodeRecordMap
* @return
*/
private List<Map<String, String>> mergeNode(List<Map<String, String>> nodes,Map<String, NodeMapRecord> nodeRecordMap) {
private List<Map<String, String>> mergeNode(List<Map<String, String>> nodes, Map<String, NodeMapRecord> nodeRecordMap) {
return nodes.stream().map(map -> {
Map<String, String> nodeMap = new HashMap<>();
@ -373,11 +406,12 @@ public class Neo4jServiceImpl implements Neo4jService {
nodeMap.put("id", nodeMapRecord.id);
return nodeMap;
}).filter(map -> StrUtil.isNotEmpty(map.get("id")))
.filter(distinctPredicate(m->m.get("id"))).collect(Collectors.toList());
.filter(distinctPredicate(m -> m.get("id"))).collect(Collectors.toList());
}
/**
*
*
* @param webRelDTOList
* @param nodeRecordMap
* @return
@ -400,7 +434,7 @@ public class Neo4jServiceImpl implements Neo4jService {
.filter(entry -> entry.getValue().idSet.contains(target))
.findAny().map(Map.Entry::getKey).orElse("");
if (StrUtil.isEmpty(sourceNew) || StrUtil.isEmpty(targetNew)){
if (StrUtil.isEmpty(sourceNew) || StrUtil.isEmpty(targetNew)) {
log.warn("mergerWebRel:关系信息异常nodeRecordMap中不存在节点id:{}或节点id:{}信息,节点名称为:{}", source, target, name);
}
@ -409,9 +443,9 @@ public class Neo4jServiceImpl implements Neo4jService {
.filter(distinctPredicate(rel -> rel.getSource() + rel.getTarget())).toList();
}
private <K> Predicate<K> distinctPredicate(Function<K,Object> function){
private <K> Predicate<K> distinctPredicate(Function<K, Object> function) {
ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
return (t)-> null == map.putIfAbsent(function.apply(t),true);
return (t) -> null == map.putIfAbsent(function.apply(t), true);
}

Loading…
Cancel
Save