[英]Neo4J - Copy all relationships from one to another node (C# Wrapper)
我目前正在努力解决一个问题,目前我还没有找到任何解决方法。 (我正在使用 NEO4J C# 库)
我需要将两个节点合并到第三个节点中,并将这两个节点中的所有关系(类型和属性)复制到我新创建的第三个节点:
(a:Label)-[r]-()
(b:Label)-[r2]-()
(c:Label)
我已经能够正确检索我的两个第一个节点并将其合并到在数据库中创建的第三个节点中,但我正在努力将所有关系从两个第一个节点复制到第三个节点。
我尝试了几件事但没有成功,例如:
using (GraphClient graphClient = GetGraphClient())
{
var inputString = string.Format("({0}:{1})-[r]->(n), ({2}:{3})", "a", typeof(Label).Name, "b", typeof(Label).Name);
var query = graphClient.Cypher
.SendQueryOnMaster()
.Match(inputString)
.Where((Label a) => a.Id == from.Id)
.AndWhere((Label b) => b.Id == to.Id)
.Create("(b)-[r2:type(r)]->(n)");
query.ExecuteWithoutResults();
}
将所有关系从一个节点复制到另一个节点可能是人们可能遇到的用例:)
有没有办法让它工作?
谢谢
更新,我找到了一种使用 C# 包装器将所有关系从一个节点复制到另一个节点的方法。
internal static void DuplicateRelationships<T>(T from, T to) where T : IDataObject
{
string aVariable = "a";
string bVariable = "b";
string nVariable = "n";
string relationVariable = "r";
string newRelation = "r2";
string relationsVariable = "rels";
string relationPostCollectVariable = "rel";
Guid fromId = from.Id;
Guid toId = to.Id;
foreach (string relation in CypherVerbs.GetAllVerbs())
{
using (GraphClient graphClient = GetGraphClient())
{
/*-[r]->*/
graphClient.Cypher
.SendQueryOnMaster()
.Match(string.Format("({0}:{1})-[{2}:{3}]->({4}), ({5}:{6})", aVariable, from.GetType().Name, relationVariable, relation, nVariable, bVariable, to.GetType().Name))
.Where((T a) => a.Id == fromId)
.AndWhere((T b) => b.Id == toId)
.With(string.Format("COLLECT({0}) AS {1}, {2}, {3}, {4}", relationVariable, relationsVariable, aVariable, bVariable, nVariable))
.ForEach(string.Format("({0} in {1} | ", relationPostCollectVariable, relationsVariable))
.Create(string.Format("({0})-[{1}:{2}]->({3})", bVariable, newRelation, relation, nVariable))
.Set(string.Format("{0} += {1})", newRelation, relationPostCollectVariable))
.ExecuteWithoutResults();
/*<-[r]-*/
graphClient.Cypher
.SendQueryOnMaster()
.Match(string.Format("({0}:{1})<-[{2}:{3}]-({4}), ({5}:{6})", aVariable, from.GetType().Name, relationVariable, relation, nVariable, bVariable, to.GetType().Name))
.Where((T a) => a.Id == fromId)
.AndWhere((T b) => b.Id == toId)
.With(string.Format("COLLECT({0}) AS {1}, {2}, {3}, {4}", relationVariable, relationsVariable, aVariable, bVariable, nVariable))
.ForEach(string.Format("({0} IN {1} | ", relationPostCollectVariable, relationsVariable))
.Create(string.Format("({0})<-[{1}:{2}]-({3})", bVariable, newRelation, relation, nVariable))
.Set(string.Format("{0} += {1})", newRelation, relationPostCollectVariable))
.ExecuteWithoutResults();
}
}
}
这是密码:
MATCH (a:Test {props1:"1"}), (b:Test {props3:"3"})
WITH a,b
MATCH (a)-[r:LINKED_TO]->(c)
WITH COLLECT(r) AS rels, a, b, c
FOREACH (rel in rels |
CREATE (b)-[r:LINKED_TO]->(c)
SET r+=rel
)
编辑:从 Neo4j 3.0 开始,您可以使用存储过程来更有效地执行此操作,Michael Hunger 开发了几个模板存储过程,包括将所有关系从一个节点复制到另一个过程。
这是存储过程存储库的链接: https : //github.com/neo4j-contrib/neo4j-apoc-procedures
这是图形重构文档的链接: https : //neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_refactoring
不幸的是,这在Neo4j
是不可能的,我想你已经在 StackOverflow 上找到了其他帖子:
如果您在编译时知道关系的 Label 是可能的,但它不适用于多个/动态标签类型。 很抱歉,没有更好的答案可以给您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.