使用 py2neo 更新具有新关系的 neo4j 节点

Update neo4j node with new relations, using py2neo

我的代码添加点头并创建关系。它在连接为 a->b、a->c、a->d 时更新,它有效(向节点添加新关系),但是当我添加连接 f->a 时,名称为 a 的第二个节点是创建。我怎样才能让它更新现有的节点a?

graph = Graph()
with open('test2') as fp:
    for line in fp:
        result = line.split('\t')
        category1 = graph.merge_one("Category", "name",result[0][result[0].rfind(':')+1:])
        category2 = graph.merge_one("Category", "name",result[1][result[1].rfind(':')+1:])
        print result[0][result[0].rfind(':')+1:] +"|"+result[1][result[1].rfind(':')+1:]
        graph.create_unique(Path(category1, "SubCategoryOf", category2))

我的测试文件是:

Category:Wars_involving_Burma Category:Wars_by_country Category:Wars_involving_Burma Category:Military_history_of_Burma Category:Wars_involving_Burma Category:Foreign_relations_of_Burma Category:World_War_II Category:Wars_involving_Bulgaria Category:World_War_II Category:Wars_involving_Burma

在这个例子中

Category:Wars_involving_Burma

创建了两次。

当我运行你的例子时,我没有得到一个节点两次。根据您的问题,我无法判断每行中有多少 'Category:...'。从你分割线的方式来看,我认为它总是两条线。一个可能的问题是您没有删除行尾,因此您的 'Category:Wars_involving_Burma' 节点之一可能在末尾有一个换行符。另外你粘贴的是space分开的,不是\t.

假设您的文件看起来像 http://paste.ubuntu.com/10874106/

,这里有一个如何改进代码的建议
graph = Graph()

with open('test2') as fp:
    for line in fp:
        # strip the line ending first, then split by whitespace
        # I assume every line has to category entries?
        result = line.rstrip().split()

        # getting the category name is easier and more readable like this
        category1 = graph.merge_one("Category", "name", result[0].split(':')[1])
        category2 = graph.merge_one("Category", "name", result[1].split(':')[1])

        print result[0].split(':')[1] + '\t' + result[1].split(':')[1]

        # you don't need a Path here        
        graph.create_unique((category1, "SubCategoryOf", category2))

此外,如果您希望您的 "Category" 节点是唯一的,您应该对 "Category" 节点的 "name" 属性 设置唯一性约束。

密码:

CREATE CONSTRAINT ON (n:Category) ASSERT n.name IS UNIQUE

py2neo:

graph.schema.create_uniqueness_constraint('Category', 'name')