1

我有一个 Neo4j 数据库,其中包含有关国会议员的信息。我遇到的问题是是否有空缺职位。发生这种情况时,我在“Congressmen”索引中使用相同的键:值。我尝试了下面的代码,因为在 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方法当然是幂等的:同一实体只能添加一次到特定入口点。然而GraphDatabaseService.create方法不是。每次运行该create方法时,都会创建一个新节点,并且每次运行都会add将该新节点附加到索引中。您可能想改用Index.add_if_none, Index.create_if_noneorIndex.get_or_create方法。

于 2013-08-13T21:04:31.067 回答