apoc.refactor.from过程
语法 |
|
||
描述 |
将给定的 |
||
输入参数 |
名称 |
类型 |
描述 |
|
|
要重定向的关系。 |
|
|
|
要将关系重定向到的节点。 |
|
|
|
|
|
返回参数 |
名称 |
类型 |
描述 |
|
|
原始实体的内部 ID。 |
|
|
|
复制后的实体。 |
|
|
|
复制过程中发生的任何错误。 |
|
使用 Cypher 重构节点
在 Cypher 中,无需使用 APOC 即可动态引用节点标签和关系类型。
用于动态创建、匹配和合并标签与类型的 Cypher 语法
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))
动态计算出的类型必须求值为 STRING 或 LIST<STRING>。更多信息,请参阅 Cypher 手册 → CREATE、MERGE、MATCH。
使用示例
本节中的示例基于以下示例图
CREATE (mark:Person {name: "Mark", city: "London"})
CREATE (jennifer:Person {name: "Jennifer", city: "St Louis"})
CREATE (michael:Person {name: "Michael", city: "Dresden"})
CREATE (mark)-[:FOLLOWS]->(jennifer);
以下示例演示如何使用 APOC 和 Cypher 将 Michael 设为 FOLLOWS 关系的起始节点
apoc.refactor.from
MATCH (michael:Person {name: "Michael"})
MATCH ()-[rel:FOLLOWS]->()
CALL apoc.refactor.from(rel, michael, { failOnErrors: true })
YIELD input, output
RETURN input, output;
使用 Cypher
MATCH (michael:Person {name: "Michael"})
MATCH ()-[rel:FOLLOWS]->()
CALL (rel, michael) {
WITH id(rel) AS oldId, properties(rel) AS relProps, type(rel) AS relType, endNode(rel) AS endNode
DELETE rel
MERGE (michael)-[newRel:$(relType)]->(endNode)
SET newRel = relProps
RETURN oldId AS oldId, newRel AS newRel
}
RETURN oldId, newRel
| input | 输出 |
|---|---|
14 |
[:FOLLOWS] |
我们可以运行以下查询来列出所有 Person 节点
MATCH path = ()-[rel:FOLLOWS]->()
RETURN path;
| path |
|---|
(:Person {name: "Michael", city: "Dresden"})-[:FOLLOWS]→(:Person {name: "Jennifer", city: "St Louis"}) |