1

下院議員に関する情報を含む 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 回実行した後にインターフェイスを確認すると、ノードが重複しています。ここに画像の説明を入力

ノードの重複を防ぎ、同じキー/値を使用してインデックスを作成することはできますか?

4

1 に答える 1

2

このIndex.addメソッドは確かに冪等です。同じエンティティを特定のエントリ ポイントに追加できるのは 1 回だけです。ただし、GraphDatabaseService.create方法はそうではありません。メソッドを実行するたびcreateに新しいノードが作成され、 を実行するたびにaddその新しいノードがインデックスに追加されます。Index.add_if_none代わりに、Index.create_if_noneまたはIndex.get_or_createメソッドを使用することをお勧めします。

于 2013-08-13T21:04:31.067 に答える