Search code examples
pythongraphneo4jpy2neo

Which are the equivalent methods of py2neo-v2 merge_one, create_unique, find_one for py2neo v3?


I recently switched my py2neo library from version V2 to version V3 and I don't know the new commands to perform certain opertions.

In particular I am stuck with:

graph.merge_one

Match or create a node by label and optional property and return a single matching node. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. (read the docs)

e.g.

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 one or more unique paths or relationships in a single transaction. This is similar to create() but uses a Cypher CREATE UNIQUE clause to ensure that only relationships that do not already exist are created. (read the docs)

e.g.

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

Find a single node by label and optional property. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. (read the docs)

e.g.

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?

I found out that these methods are no longer available in py2neo V3,
so what's the equivalent of these methods for py2neo V3 ?


Solution

  • These are the equivalent methods for py2neo V3 (and also for py2neo V.2020.1)

    • merge_one -> merge

    • create_unique -> in you example it is simply merge, because this is sufficient to ensure that

      only relationships that do not already exist are created.

    • find_one ->

      this is more complex and has to be splitted into more operations,
      you can do it with NodeMatcher function and match method.
      I'll show it directly in the example below.

    Here are your examples changed to py2neo V3 commands:

    graph.merge_one

    Match or create a node by label and optional property and return a single matching node. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. ([read the docs][3])

    e.g.

    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 one or more unique paths or relationships in a single transaction. This is similar to create() but uses a Cypher CREATE UNIQUE clause to ensure that only relationships that do not already exist are created. ([read the docs][4])

    e.g.

    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

    Find a single node by label and optional property. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. ([read the docs][5])

    e.g.

    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()