【问题标题】:Which are the equivalent methods of py2neo-v2 merge_one, create_unique, find_one for py2neo v3?py2neo-v2 merge_one、create_unique、find_one 的等效方法有哪些?
【发布时间】:2020-12-13 01:42:23
【问题描述】:

我最近将我的 py2neo 库从 version V2 切换到 version V3,但我不知道执行某些操作的新命令。

特别是我坚持:

graph.merge_one

通过标签和可选属性匹配或创建一个节点并返回一个 单个匹配节点。此方法旨在与独特的 如果找到多个匹配节点,则不会失败。 (read the docs)

例如

from py2neo import Node, Graph
nicole = Node("Person", name="Nicole", age=24) 

# adds the nicole element to the graph if it does not already exist a node labelled as "Person" having attribute "name" equal to "Nicole".
graph.merge_one("Person", "name", "Nicole")     #<-- What's the equivalent py2neo V3 command?

graph.create_unique

在单个文件中创建一个或多个独特的路径或关系 交易。这类似于 create() 但使用 Cypher CREATE UNIQUE 子句确保只有那些还没有的关系 存在被创建。 (read the docs)

例如

from py2neo import Node, Relationship, Graph

kenny = Node("Person", name="Kenny")
graph.create(kenny)

kingfish = Node("Bar", name="Kingfish")
graph.create(kingfish)

rel = Relationship(kenny, "LIKES", kingfish)

# creates the relationship (kenny)-[:LIKES]->(kingfish), but only if it does not exist yet.
graph.create_unique(rel)     #<-- What's the equivalent py2neo V3 command?

graph.find_one

通过标签和可选属性查找单个节点。这种方法是 旨在与唯一约束一起使用,如果更多则不会失败 多于一个匹配节点。 (read the docs)

例如

from py2neo import Graph

# Find one node (and take only one if there are many) matching these conditions: its type is "Person" and it has an attribute "name" equal to "Kenny", and then save it inside "kenny" variable.
kenny = graph.find_one("Person", "name", "Kenny")      #<-- What's the equivalent py2neo V3 command?

我发现这些方法在 py2neo V3 中不再可用,
那么这些方法对于 py2neo V3 的等价物是什么?

【问题讨论】:

    标签: python graph neo4j py2neo


    【解决方案1】:

    这些是 py2neo V3 的等效方法(也适用于 py2neo V.2020.1

    • merge_one -> merge

    • create_unique -> 在你的例子中它只是merge,因为这个 足以确保

      只创建不存在的关系。

    • find_one->

      这更复杂,必须拆分为更多操作,
      您可以使用NodeMatcher 函数和match 方法来完成。
      我会在下面的例子中直接展示。

    以下是将您的示例更改为 py2neo V3 命令:

    graph.merge_one

    通过标签和可选属性匹配或创建一个节点并返回一个 单个匹配节点。此方法旨在与独特的 如果找到多个匹配节点,则不会失败。 ([阅读文档][3])

    例如

    from py2neo import Node, Graph
    nicole = Node("Person", name="Nicole", age=24) 
    
    # adds the nicole element to the graph if it does not already exist a node labelled as "Person" having attribute "name" and "Person" and having their values equal to those of "nicole" element.
    graph.merge(nicole, "Person", "name") 
    

    graph.create_unique

    在单个文件中创建一个或多个独特的路径或关系 交易。这类似于 create() 但使用 Cypher CREATE UNIQUE 子句确保只有那些还没有的关系 存在被创建。 ([阅读文档][4])

    例如

    from py2neo import Node, Relationship, Graph
    
    kenny = Node("Person", name="Kenny")
    graph.create(kenny)
    
    kingfish = Node("Bar", name="Kingfish")
    graph.create(kingfish)
    
    rel = Relationship(kenny, "LIKES", kingfish)
    
    # creates the relationship (kenny)-[:LIKES]->(kingfish), but only if it does not exist yet.
    graph.merge(rel)
    

    graph.find_one

    通过标签和可选属性查找单个节点。这种方法是 旨在与唯一约束一起使用,如果更多则不会失败 多于一个匹配节点。 ([阅读文档][5])

    例如

    from py2neo import Graph
    
    # Find one node (and take only one if there are many) matching these conditions: its type is "Person" and it has an attribute "name" equal to "Kenny", and then save it inside "kenny" variable.
    from py2neo import NodeMatcher
    matcher = NodeMatcher(graph)
    kenny = matcher.match('Person', name='Kenny').first()
    

    【讨论】:

      猜你喜欢
      • 2019-03-31
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多