简体   繁体   English

如何在现有的两个节点之间建立关系,我正在使用neo4j。

[英]how to create relation between existing two node, I'm using neo4j.

I'm in starting neo4j and I'm using python3.5 and py2neo. 我正在启动neo4j,我正在使用python3.5和py2neo。

I had build two graph node with following code. 我用以下代码构建了两个图形节点。 and successfully create.[! 并成功创造。[!

>>> u1 = Node("Person",name='Tom',id=1)
>>> u2 = Node('Person', name='Jerry', id=2)
>>> graph.create(u1,u2)

在此输入图像描述

after that, I going to make a relation between 'Tom' and 'Jerry' Tom's id property is 1, Jerry's id property is 2. 之后,我将在'Tom'和'Jerry'之间建立关系Tom的id属性为1,Jerry的id属性为2。

So. 所以。 I think, I have to point to existing two node using id property. 我想,我必须指向使用id属性的现有两个节点。 and then I tried to create relation like below. 然后我尝试创建如下的关系。

>>> u1 = Node("Person",id=1)
>>> u2 = Node("Person",id=2)
>>> u1_knows_u2=Relationship(u1, 'KKNOWS', u2)
>>> graph.create(u1_knows_u2)

above successfully performed. 以上成功完成。 But the graph is something strange. 但图表有些奇怪。

在此输入图像描述

I don't know why unknown graph nodes are created. 我不知道为什么会创建未知的图节点。 and why the relation is created between unknown two node. 以及为什么在未知的两个节点之间创建关系。

You can have two nodes with the same label and same properties. 您可以拥有两个具有相同标签和相同属性的节点。 The second node you get with u1 = Node("Person",id=1) is not the same one you created before. 使用u1 = Node("Person",id=1)获得的第二个节点与您之前创建的u1 = Node("Person",id=1)不同。 It's a new node with the same label/property. 这是一个具有相同标签/属性的新节点。

When you define two nodes (ie your new u1 and u2 ) and create a relationships between them, the whole pattern will be created. 当您定义两个节点(即新的u1u2 )并在它们之间创建关系时,将创建整个模式。

To get the two nodes and create a relationship between them you would do: 要获得两个节点并在它们之间创建关系,您可以:

# create Tom and Jerry as before
u1 = Node("Person",name='Tom',id=1)
u2 = Node('Person', name='Jerry', id=2)
graph.create(u1,u2)

# either use u1 and u2 directly
u1_knows_u2 = Relationship(u1, 'KKNOWS', u2)
graph.create(u1_knows_u2)

# or find existing nodes and create a relationship between them
existing_u1 = graph.find_one('Person', property_key='id', property_value=1)
existing_u2 = graph.find_one('Person', property_key='id', property_value=2)

existing_u1_knows_u2 = Relationship(existing_u1, 'KKNOWS', existing_u2)
graph.create(existing_u1_knows_u2)

find_one() assumes that your id properties are unique. find_one()假定您的id属性是唯一的。

Note also that you can use the Cypher query language with Py2neo: 另请注意,您可以将Cypher查询语言与Py2neo一起使用:

graph.cypher.execute('''
   MERGE (tom:Person {name: "Tom"})
   MERGE (jerry:Person {name: "Jerry"})
   CREATE UNIQUE (tom)-[:KNOWS]->(jerry)
''')

The MERGE statement in Cypher is similar to "get or create". Cypher中的MERGE语句类似于“获取或创建”。 If a Person node with the given name "Tom" already exists it will be bound to the variable tom , if not the node will be created and then bound to tom . 如果具有给定名称“Tom”的Person节点已经存在,则它将绑定到变量tom ,否则将创建节点然后绑定到tom This, combined with adding uniqueness constraints allows for avoiding unwanted duplicate nodes. 这与添加唯一性约束相结合,可以避免不必要的重复节点。

检查此查询,

MATCH (a),(b) WHERE id(a) =1 and id(b) = 2 create (a)-[r:KKNOWS]->(b) RETURN a, b

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

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