简体   繁体   English

py2neo,neo4j:如何在两个现有节点之间建立关系

[英]py2neo, neo4j: How to create relation between two existing node

I am following this tutorial to access neo4j db using python. 我正在按照本教程使用python访问neo4j db。 According to this tutorial I have created 2 relations among 4 nodes. 根据这个教程,我在4个节点之间创建了2个关系。 The code is given below 代码如下

alice, bob, rel = graph_db.create(
                      {"name": "Alice"}, {"name": "Bob"},
                      (0, "KNOWS", 1))
dev, carol, rel = graph_db.create(
                      {"name": "Dev"}, {"name": "Carol Smith"},
                      (0, "KNOWS", 1))

How can I create relation between alice and carol without creating new node? 如何在不创建新节点的情况下创建alice和carol之间的关系?

Following code snippet is given in that tutorial to create relation between existing node. 下面的代码片段在该教程中给出,用于创建现有节点之间的关系。 Not sure how to use how to use this in above case. 在上面的例子中不确定如何使用它。

ref_node = graph_db.get_reference_node()
alice, rel = graph_db.create(
                {"name": "Alice"}, (ref_node, "PERSON", 0))

When I try to execute 当我尝试执行时

ref_node = graph_db.get_reference_node()

I get the following error. 我收到以下错误。

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'GraphDatabaseService' object has no attribute 'get_reference_node'

Any suggestion to solve this issue? 有什么建议可以解决这个问题吗?

I tried the following and got the results I think you want: 我尝试了以下内容并得到了我认为你想要的结果:

from py2neo import neo4j, node, rel

graph = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

alice, = graph.create(node(name="Alice")) # Comma unpacks length-1 tuple.
bob, = graph.create(node(name="Bob"))
carol, = graph.create(node(name="Carol Smith"))
dev, = graph.create(node(name="Dev"))

graph.create(rel(alice, "KNOWS", bob))
graph.create(rel(dev, "KNOWS", carol))
graph.create(rel(alice, "KNOWS", carol))

My graph now looks like this in the browser: 我的图表现在在浏览器中显示如下:

在此输入图像描述

Alternatively, you can create the graph in one graph.create() statement: 或者,您可以在一个graph.create()语句中创建图形:

from py2neo import neo4j, node, rel

graph = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

graph.create(
    node(name="Alice"),       #0
    node(name="Bob"),         #1
    node(name="Carol Smith"), #2
    node(name="Dev"),         #3
    rel(0, "KNOWS", 1),
    rel(3, "KNOWS", 2),
    rel(0, "KNOWS", 2)
)

And the output is the same. 输出是一样的。 Hope this helps. 希望这可以帮助。

The reference node was a feature included in earlier versions of Neo4j and, by extension, py2neo. 参考节点是早期版本的Neo4j中包含的功能,并且扩展名为py2neo。 It has since been deprecated and removed so I should have also removed all traces from the py2neo documentation - it appears I've missed one! 它已经被弃用和删除所以我也应该从py2neo文档中删除所有痕迹 - 看来我错过了一个!

Thanks for pointing this out, I'll add myself a task to get this page up to date. 感谢您指出这一点,我将为自己添加一项任务,以使此页面更新。

In terms of creating a relationship, Nicole's answer is spot on and should have all the information you need. 在建立关系方面,妮可的答案是现场,并且应该拥有您需要的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM