简体   繁体   English

py2neo graph.merge()与Cypher MERGE的行为有何不同?

[英]py2neo graph.merge() behaves differently from Cypher MERGE?

So, for an empty database MERGE (N1:A {name:"A"})-[:r]->(N2:B {name:"B"}) will create two nodes N1 and N2 with an edge r between them. 因此,对于空数据库MERGE (N1:A {name:"A"})-[:r]->(N2:B {name:"B"})将创建两个节点N1N2 ,其边缘为r他们。 The following python code however does not do that... but why? 但是下面的python代码没有这样做......但为什么呢? Should it not? 不应该吗?

from py2neo import Graph, authenticate, rel, Node

graph = Graph()

# set up authentication parameters
authenticate("localhost:7474", <user>, <password>)

# clear the data base
graph.delete_all()

graph.merge(rel(Node("A" , name="A"), "r", Node("B" , name="B")))

Running that script results in a still empty database. 运行该脚本会导致数据库仍为空。 Why is that and how can I get the Cypher merge behaviour from py2neo without using graph.cypher.execute("MERGE ...") ? 为什么这样,如何在不使用graph.cypher.execute("MERGE ...")情况下从py2neo获取Cypher合并行为?

In Py2neo graph.merge matches or creates a single node by label and (optionally) property, where you are wanting to MERGE on the entire pattern (node, relationship, other node). 在Py2neo中, graph.merge通过label和(可选)属性匹配或创建单个节点,您希望在整个模式(节点,关系,其他节点)上进行MERGE。

The pattern you are using for the Cypher MERGE statement does not appear to be supported in Py2neo outside of Cypher. 您在Cypher MERGE语句中使用的模式似乎在Cypher之外的Py2neo中不受支持。

Here is a example on how to merge a relationship of two nodes. 这是一个关于如何合并两个节点的关系的示例。

from py2neo import Graph, authenticate, Relationship, Node

server = "localhost:7474"

# set up authentication parameters
authenticate(server, <user>, <password>)

graph = Graph("{0}/db/data".format(server))

# merge nodes and relationship
node1 = Node("A", name="A")
node2 = Node("B", name="B")
node1_vs_node2 = Relationship(node1, "r", node2)
graph.merge(node1_vs_node2)

The result is: 结果是: 合并后的节点A和B相关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM