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 | output |
|---|---|
14 |
[:FOLLOWS] |
我们可以通过运行以下查询列出所有 Person 节点
MATCH path = ()-[rel:FOLLOWS]->()
RETURN path;
| 路径 |
|---|
(:Person {name: "Michael", city: "Dresden"})-[:FOLLOWS]→(:Person {name: "Jennifer", city: "St Louis"}) |