Question

I have a Neo4j database containing information on Congressmen. The problem I'm having is if there is a vacant position. When this happens I am using the same key:value in the "Congressmen" index. I tried the code below because in the py2neo documentation it states that the add function is idempotent

#Check if we have any vacancies and if so if they match the one that we currently want to add
    query="start n=node:Congressmen('website:N/A') return n"
    result= cypher.execute(graph_db, query.encode('utf-8', errors='ignore'))

    #Match what  we already have
    if str(result[0]) != "[]":
        #create is idempotent so will only create a new node if properties are different
        rep, = graph_db.create({"name" : userName, "website" : web, "district" : int(district), "state" : child[2].text, "party" : child[4].text, "office" : child[5].text, "phone" : child[6].text, "house" : "House of Representatives"})
        cong = graph_db.get_or_create_index(neo4j.Node, "Congressmen")

        # add the node to the index
        cong.add("website", web, rep)

When I checked the interface after running the code 3 times I have duplicate nodes.enter image description here

Is it possible to prevent the nodes from duplicating and still be able to index them using the same key/value?

Was it helpful?

Solution

The Index.add method is certainly idempotent: the same entity can only be added once to a particular entry point. The GraphDatabaseService.create method is not however. Each time you run the create method, a new node is created and each run of add appends that new node to the index. You probably want to use the Index.add_if_none, Index.create_if_none or Index.get_or_create method instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top