py2neo创建函数创建重复节点

时间:2013-08-13 17:20:18

标签: neo4j py2neo

我有一个Neo4j数据库,其中包含有关国会议员的信息。我遇到的问题是,如果有空缺职位。当发生这种情况时,我使用相同的密钥:“国会议员”索引中的值。我尝试了下面的代码,因为在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次后检查界面时,我有重复的节点。enter image description here

是否可以阻止节点重复并仍然能够使用相同的键/值对它们进行索引?

1 个答案:

答案 0 :(得分:2)

Index.add方法肯定是幂等的:同一个实体只能添加一次到特定的入口点。但是GraphDatabaseService.create方法不是。每次运行create方法时,都会创建一个新节点,每次运行add会将该新节点附加到索引。您可能希望改用Index.add_if_noneIndex.create_if_noneIndex.get_or_create方法。

相关问题