py2neo在现有节点之间建立新的边缘

时间:2016-03-17 10:28:01

标签: python neo4j cypher edge py2neo

让我们说我想在两个相邻节点之间将新边合并到我的图形中(这样在操作之后,所讨论的边缘可以加倍)。

我有graph.cypher.execute(<some query>)返回的以下记录:

   | p                         
---+----------------------------
 1 | (:A)-[:r]->(:B)-[:r]->(:C)

现在我想将边缘(A,B)和(B,C)加倍。为此我写了这段代码:

for record in graph.cypher.execute(<some query>):
    for rel in record[0]:
            self.graph.cypher.execute("MERGE "+str(self.graph.node(rel.start_node.ref))+"-[:new]->"+str(self.graph.node(rel.end_node.ref)))

但是,我没有在现有节点之间获得新的边缘,而是两个新的关系,总共有4个新节点,因为显然NEO4J没有将str(self.graph.node(rel.start_node.ref))str(self.graph.node(rel.end_node.ref))解释为引用现有节点在图中。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

self.graph.create(rel(r.start_node, "new", r.end_node))做到了。

需要from py2neo import rel