使用 Cypher 添加与现有节点的关系 [英] Adding relationship to existing nodes with Cypher

查看:19
本文介绍了使用 Cypher 添加与现有节点的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次尝试 Neo4j.我使用的是 2.0-RC1 社区版.

I'm trying out Neo4j for the first time. I'm using the 2.0-RC1 community edition.

我创建了一些节点:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆