Search code examples
neo4jpy2neo

Nodes are getting repeated when I use Neo4j and py2neo


I have a set of list and the list contains repeated items. for example tag_data = ['a','b','c','d','c','a','f','b','a'] etc.... Now I have to use neo4j database and py2neo to store these list items. So I tried

graph = Graph("http://......./db/data")
graph.schema.create_uniqueness_constraint('Zigzag', 'tagName')
for i,tags in enumerate(tag_data):
    var_tag = "tag"+str(i)
    var_tag = Node("Zigzag",tagName=tags)
    graph.create(var_tag)

When I tried the below code I dont see the uniqueness in the graph. So I tried to use find and find_one method to get the uniqueness. But I am getting an error find and find_one are not the graph method.

I referred some of the links in stackoverflow and I tried match and merge that is also not working

for i,tags in enumerate(tag_data):
    var_tag = "tag"+str(i)
    print(var_tag)
    matcher = NodeMatcher(graph)
    m = matcher.match("Zigzag",tags).first()
    print(m,"hi")
    if m is None:
        var_tag = Node("Zigzag",tagName=tags)
        graph.create(var_tag)

with merge

for i,tags in enumerate(tag_data):
    var_tag = "tag"+str(i)
    print(var_tag)                
    graph.merge("Zigzag","tagName",tags)

How do I create the unique nodes in neo4j(4.0.3) and py2neo.


Solution

  • Assuming tags are labelled only by their name, here is a working example using graph.merge from py2neo:

    from py2neo import Graph, Node
    
    graph = Graph("....")
    
    tags = ['a','b','c','d','c','a','f','b','a']
    for tag in tags:
        n = Node("Zigzag", tagName=tag)  # create node object
        graph.merge(n, "Zigzag", "tagName")  # merge node into graph using Zigzag as primary label and tagName property as primary key
    

    So, if a node with label "Zigzag" and the same "tagName" property already exists, it won't be created again, otherwise, the node is created.

    If you add a print at the end of the loop:

    print(tag, n)
    

    You will see that all nodes with the same "tagName" have the same ID:

    a (_4:Zigzag {tagName: 'a'})
    b (_5:Zigzag {tagName: 'b'})
    c (_6:Zigzag {tagName: 'c'})
    d (_7:Zigzag {tagName: 'd'})
    c (_6:Zigzag {tagName: 'c'})
    a (_4:Zigzag {tagName: 'a'})
    f (_8:Zigzag {tagName: 'f'})
    b (_5:Zigzag {tagName: 'b'})
    a (_4:Zigzag {tagName: 'a'})