【问题标题】:py2neo graph.merge() behaves differently from Cypher MERGE?py2neo graph.merge() 的行为与 Cypher MERGE 不同?
【发布时间】:2016-07-06 07:55:02
【问题描述】:

因此,对于一个空数据库MERGE (N1:A {name:"A"})-[:r]->(N2:B {name:"B"}) 将创建两个节点N1N2,它们之间有一条边r。但是,以下 python 代码 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")))

运行该脚本会导致数据库仍然为空。为什么会这样?如何在不使用 graph.cypher.execute("MERGE ...") 的情况下从 py2neo 获得 Cypher 合并行为?

【问题讨论】:

    标签: python merge neo4j cypher py2neo


    【解决方案1】:

    在 Py2neo 中,graph.merge 通过标签和(可选)属性匹配或创建单个节点,您希望在整个模式(节点、关系、其他节点)上合并。

    Cypher 之外的 Py2neo 似乎不支持您用于 Cypher MERGE 语句的模式。

    【讨论】:

      【解决方案2】:

      这是一个关于如何合并两个节点的关系的示例。

      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)
      

      结果是:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多