Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4j 在py2neo中连接具有关系的节点_Neo4j_Py2neo - Fatal编程技术网

Neo4j 在py2neo中连接具有关系的节点

Neo4j 在py2neo中连接具有关系的节点,neo4j,py2neo,Neo4j,Py2neo,我使用以下python代码在neo4j中创建一个图形。我使用的是py2neo版本2.0.3 import json from py2neo import neo4j, Node, Relationship, Graph graph = neo4j.Graph("http://localhost:7474/db/data/") with open("example.json") as f: for line in f: wh

我使用以下python代码在neo4j中创建一个图形。我使用的是py2neo版本2.0.3

 import json
    from py2neo import neo4j, Node, Relationship, Graph

    graph = neo4j.Graph("http://localhost:7474/db/data/")
       with open("example.json") as f:
        for line in f:
            while True:
                try:
                    file = json.loads(line)
                    break
                except ValueError:
                    # Not yet a complete JSON value
                    line += next(f)

                    # Now creating the node and relationships

            news, = graph.create(Node("Mainstream_News", id=unicode(file["_id"]), entry_url=unicode(file["entry_url"]),
                                      title=unicode(file["title"])))  # Comma unpacks length-1 tuple.
            authors, = graph.create(
                Node("Authors", auth_name=unicode(file["auth_name"]), auth_url=unicode(file["auth_url"]),
                     auth_eml=unicode(file["auth_eml"])))

            graph.create(Relationship(news, "hasAuthor", authors ))
我可以创建一个带有节点的图,其中包含新闻和具有关系“hasAuthor”的作者。我的问题是,当我这样做时,我有一个主流新闻节点和一个作者,但实际上一个作者节点有多个主流新闻。我想将auth_name属性作为与主流新闻节点连接的索引。任何建议都很好

每次通过循环都会创建一个新的Authors节点,即使已经存在具有相同属性的Author节点

首先,我认为您应该对Authorsauth_name和主流新闻ID创建唯一性约束,以强制执行您的需求。这只需要做一次。唯一性约束还会自动为您创建索引,这是一个额外的好处

graph.schema.create_uniqueness_constraint("Authors", "auth_name")
graph.schema.create_uniqueness_constraint("Mainstream_News", "id")
但您可能必须首先清空数据库,至少要清空所有作者和主流新闻节点及其关系,因为我认为它当前有很多重复节点

然后,您可以使用merge_one并创建唯一的API,以防止重复的节点和关系:

news = graph.merge_one("Mainstream_News", "id", unicode(file["_id"]))
news.properties["entry_url"] = unicode(file["entry_url"])
news.properties["title"] = unicode(file["title"])

authors = graph.merge_one("Authors", "auth_name", unicode(file["auth_name"]))
news.properties["auth_url"] = unicode(file["auth_url"])
news.properties["auth_eml"] = unicode(file["auth_eml"])

graph.create_unique(Relationship(news, "hasAuthor", authors))
每次通过循环都会创建一个新的Authors节点,即使已经存在具有相同属性的Author节点

首先,我认为您应该对Authorsauth_name和主流新闻ID创建唯一性约束,以强制执行您的需求。这只需要做一次。唯一性约束还会自动为您创建索引,这是一个额外的好处

graph.schema.create_uniqueness_constraint("Authors", "auth_name")
graph.schema.create_uniqueness_constraint("Mainstream_News", "id")
但您可能必须首先清空数据库,至少要清空所有作者和主流新闻节点及其关系,因为我认为它当前有很多重复节点

然后,您可以使用merge_one并创建唯一的API,以防止重复的节点和关系:

news = graph.merge_one("Mainstream_News", "id", unicode(file["_id"]))
news.properties["entry_url"] = unicode(file["entry_url"])
news.properties["title"] = unicode(file["title"])

authors = graph.merge_one("Authors", "auth_name", unicode(file["auth_name"]))
news.properties["auth_url"] = unicode(file["auth_url"])
news.properties["auth_eml"] = unicode(file["auth_eml"])

graph.create_unique(Relationship(news, "hasAuthor", authors))

这是我通常做的事情,因为我发现更容易知道发生了什么。据我所知,有一个,但当你只创建一个节点,没有必要创建节点,当你还必须创建一个边

我没有这台计算机上的数据库,所以请容忍我,如果有一些拼写错误,我会在早上纠正,但我想你会有一个快速的答案-

news = graph.cypher.execute_one('MATCH (m:Mainstream_News) '
    'WHERE m.id = {id} '
    'RETURN p'.format(id=unicode(file["_id"])))

if not news:
   news = Node("Mainstream_News")
   news.properties['id] = unicode(file["_id"])
   news.properties['entry_url'] = unicode(file["entry_url"])
   news.properties['title'] = unicode(file["title"])

# You can make a for-loop here
authors = Node("Authors")
authors.properties['auth_name'] = unicode(file["auth_name"])
authors.properties['auth_url'] = unicode(file["auth_url"])
authors.properties['auth_eml'] = unicode(file["auth_eml"])

rel = Relationship(new, "hasAuthor", authors)
graph.create_unique(rel)
# For-loop should end here
我已经包括了树的第一行,使它更通用。它返回一个节点对象或无

编辑:

@cybersam使用模式很酷,实现这一点,我也会尝试自己使用它..:-

您可以在此处阅读更多信息:

这是我通常做的事情,因为我发现更容易知道发生了什么。据我所知,有一个,但当你只创建一个节点,没有必要创建节点,当你还必须创建一个边

我没有这台计算机上的数据库,所以请容忍我,如果有一些拼写错误,我会在早上纠正,但我想你会有一个快速的答案-

news = graph.cypher.execute_one('MATCH (m:Mainstream_News) '
    'WHERE m.id = {id} '
    'RETURN p'.format(id=unicode(file["_id"])))

if not news:
   news = Node("Mainstream_News")
   news.properties['id] = unicode(file["_id"])
   news.properties['entry_url'] = unicode(file["entry_url"])
   news.properties['title'] = unicode(file["title"])

# You can make a for-loop here
authors = Node("Authors")
authors.properties['auth_name'] = unicode(file["auth_name"])
authors.properties['auth_url'] = unicode(file["auth_url"])
authors.properties['auth_eml'] = unicode(file["auth_eml"])

rel = Relationship(new, "hasAuthor", authors)
graph.create_unique(rel)
# For-loop should end here
我已经包括了树的第一行,使它更通用。它返回一个节点对象或无

编辑:

@cybersam使用模式很酷,实现这一点,我也会尝试自己使用它..:-

您可以在此处阅读更多信息:

非常好地指向唯一约束实现,但在使用merge\u one:TypeError:merge\u one最多使用4个参数8 givevery非常好地指向唯一约束实现,但在使用merge\u one:TypeError:merge\u one最多使用4个参数8 gived