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

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

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

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

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

@ -12,8 +12,8 @@ import com.supervision.neo4j.service.Neo4jService;
import com.supervision.neo4j.utils.Neo4jUtils; import com.supervision.neo4j.utils.Neo4jUtils;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.*;
import org.neo4j.driver.Record; import org.neo4j.driver.Record;
import org.neo4j.driver.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -144,9 +144,7 @@ public class Neo4jServiceImpl implements Neo4jService {
CaseNode node = null; CaseNode node = null;
try { try {
Session session = driver.session(); Session session = driver.session();
StringBuffer cql = new StringBuffer(); Result run = session.run("MATCH (n) where id(n) = " + id + Neo4jUtils.NODE_RETURN);
cql.append("MATCH (n) where id(n) = ").append(id).append(Neo4jUtils.NODE_RETURN);
Result run = session.run(cql.toString());
node = Neo4jUtils.getOneNode(run); node = Neo4jUtils.getOneNode(run);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -234,14 +232,13 @@ public class Neo4jServiceImpl implements Neo4jService {
public Rel findRelation(Rel rel) { public Rel findRelation(Rel rel) {
try { try {
Session session = driver.session(); Session session = driver.session();
StringBuffer cql = new StringBuffer();
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
cql.append("MATCH (a)-[rel:").append(rel.getName()).append("]->(b) where id(a) = $sourceId and id(b) = $targetId") String cql = "MATCH (a)-[rel:" + rel.getName() + "]->(b) where id(a) = $sourceId and id(b) = $targetId" +
.append(Neo4jUtils.REL_RETURN); Neo4jUtils.REL_RETURN;
params.put("sourceId", rel.getSourceId()); params.put("sourceId", rel.getSourceId());
params.put("targetId", rel.getTargetId()); params.put("targetId", rel.getTargetId());
Result run = session.run(cql.toString(), params); Result run = session.run(cql, params);
rel = Neo4jUtils.getOneRel(run); rel = Neo4jUtils.getOneRel(run);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -258,14 +255,13 @@ public class Neo4jServiceImpl implements Neo4jService {
Rel res = null; Rel res = null;
try { try {
Session session = driver.session(); Session session = driver.session();
StringBuffer cql = new StringBuffer();
Map<String, Object> params = new HashMap<>(); 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()) String cql = "MATCH (a), (b) where id(a) = $sourceId and id(b) = $targetId CREATE(a)-[rel:" + rel.getName() +
.append("]->(b) ").append(Neo4jUtils.REL_RETURN); "]->(b) " + Neo4jUtils.REL_RETURN;
params.put("sourceId", rel.getSourceId()); params.put("sourceId", rel.getSourceId());
params.put("targetId", rel.getTargetId()); params.put("targetId", rel.getTargetId());
Result run = session.run(cql.toString(), params); Result run = session.run(cql, params);
rel = Neo4jUtils.getOneRel(run); rel = Neo4jUtils.getOneRel(run);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -320,6 +316,40 @@ public class Neo4jServiceImpl implements Neo4jService {
return R.ok(map); 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) { record NodeMapRecord(String name, String id, Set<String> idSet) {
} }
@ -327,6 +357,7 @@ public class Neo4jServiceImpl implements Neo4jService {
/** /**
* *
*
* @param nodes key: name ,entityName,id * @param nodes key: name ,entityName,id
* @return * @return
*/ */
@ -346,10 +377,12 @@ public class Neo4jServiceImpl implements Neo4jService {
} }
return nodeRecordMap; return nodeRecordMap;
} }
/** /**
* *
* *
* name * name
*
* @param nodes key: name ,entityName,id * @param nodes key: name ,entityName,id
* @param nodeRecordMap * @param nodeRecordMap
* @return * @return
@ -378,6 +411,7 @@ public class Neo4jServiceImpl implements Neo4jService {
/** /**
* *
*
* @param webRelDTOList * @param webRelDTOList
* @param nodeRecordMap * @param nodeRecordMap
* @return * @return

Loading…
Cancel
Save