使用Cypher将关系添加到现有节点 [英] Adding relationship to existing nodes with Cypher
问题描述
我是第一次尝试Neo4j.我正在使用2.0-RC1社区版.
I'm trying out Neo4j for the first time. I'm using the 2.0-RC1 community edition.
我已经创建了一些节点:
I've created some nodes:
MERGE (u:User{username:'admin',password:'admin'})
MERGE (r1:Role{name:'ROLE_ADMIN'})
MERGE (r2:Role{name:'ROLE_WEB_USER'})
MERGE (r3:Role{name:'ROLE_REST_USER'})
,现在我想在节点之间添加关系.但是,我不想要清除使用上述脚本创建的现有数据库,添加语句并再次运行它.我想将关系添加到现有节点. Google帮我找到了这个
and now I want to add relationships between the nodes. However, I don't want to clear out the existing database created with the script above, add the statements and run it again. I want to add relationships to the existing nodes. Google helped me find this:
START n=node(*), m=node(*)
where has(n.username) and has(m.name) and n.username = 'admin'
and m.name = 'ROLE_WEB_USER'
create (n)-[:HAS_ROLE]->(m)
哪个工作正常(即使我不了解所有语法).但是,我知道这会找到任何具有username属性的节点和具有name属性的节点,而不是使用标签来检查其节点类型是否正确.
Which works fine (even though I don't understand all the syntax). However, I am aware that this finds any node with a username property and any node with a name property, instead of using labels to check that it has the right type of node.
如何使用标签来做同样的事情?
How can I do the same using labels?
推荐答案
在Neo4j 2.0中,您可以为标签和用于查找的属性创建模式索引:
In Neo4j 2.0 you can create schema indexes for your labels and the properties you use for lookup:
CREATE INDEX ON :User(username)
CREATE INDEX ON :Role(name)
要创建关系,您可以使用:
To create relationships you might use:
MATCH (u:User {username:'admin'}), (r:Role {name:'ROLE_WEB_USER'})
CREATE (u)-[:HAS_ROLE]->(r)
如果可能,MATCH
将使用索引.如果没有索引,它将查找所有带有标签的节点,并查看属性是否匹配.
The MATCH
will use an index if possible. If there is no index, it will lookup up all nodes carrying the label and see if the property matches.
以上语法仅适用于Neo4j 2.0.0-RC1及更高版本.
N.B. the syntax above will only work with Neo4j 2.0.0-RC1 and above.
这篇关于使用Cypher将关系添加到现有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!