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

时间:2015-04-21 09:37:53

标签: neo4j py2neo

我的代码添加了点头并创建了关系。当连接是a-> b,a-> c,a-> d时它会更新,它可以工作(添加新节点的节点),但是当我添加连接f-> 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))

我的测试文件是:

  

分类:Wars_involving_Burma类别:Wars_by_country   分类:Wars_involving_Burma类别:Military_history_of_Burma   分类:Wars_involving_Burma类别:Foreign_relations_of_Burma   分类:World_War_II类别:Wars_involving_Bulgaria   分类:World_War_II类别:Wars_involving_Burma

在这个例子中

  

类别:Wars_involving_Burma

创建两次。

1 个答案:

答案 0 :(得分:0)

当我运行你的例子时,我没有得到一个节点两次。根据你的问题,我不知道每行中有多少'类别:......'。从你如何分割线我认为它总是两个。一个可能的问题是您不删除行结尾,因此您的“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))

此外,如果您希望“类别”节点是唯一的,则应对“类别”节点的“名称”属性具有唯一性约束。

的Cypher:

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

py2neo:

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