下院議員に関する情報を含む Neo4j データベースがあります。私が抱えている問題は、空席があるかどうかです。これが発生した場合、「Congressmen」インデックスで同じ key:value を使用しています。py2neo のドキュメントには add 関数がべき等であると記載されているため、以下のコードを試しました。
#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)
コードを 3 回実行した後にインターフェイスを確認すると、ノードが重複しています。
ノードの重複を防ぎ、同じキー/値を使用してインデックスを作成することはできますか?