Neo4j创建的图表明某些节点具有多个关系 [英] Neo4j create graph that some nodes have more than one relationships

查看:388
本文介绍了Neo4j创建的图表明某些节点具有多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Neo4j上,我要创建此图:

因此,我尝试使用以下方法创建一些节点和关系:

MERGE (D:POINT {NAME:'d'})<-[:LINKS]-(A:POINT {NAME:'a'})-[:LINKS]->(B:POINT {NAME:'b'})-[:LINKS]->(C:POINT {NAME:'c'})

但是我无法找到如何创建DB点之间的关系,也无法找到如何链接AC.

您有想法怎么做吗?

解决方案

为了避免无意中创建重复的节点和/或关系,必须在单个节点和关系上调用MERGE. /p>

引用开发人员手册:

在完整模式下使用MERGE时,其行为是 整个模式匹配,或者创建了整个模式.合并不会 部分使用现有模式-all全部或全无.如果不完整 需要匹配,这可以通过拆分模式来完成 分成多个MERGE子句.

例如,要正确创建没有任何重复节点或关系的图形:

MERGE (A:POINT {NAME:'a'})
MERGE (B:POINT {NAME:'b'})
MERGE (C:POINT {NAME:'c'})
MERGE (D:POINT {NAME:'d'})
MERGE (A)-[:LINKS]->(B)
MERGE (A)-[:LINKS]->(C)
MERGE (A)-[:LINKS]->(D)
MERGE (B)-[:LINKS]->(C)
MERGE (D)-[:LINKS]->(B)

Over my Neo4j I want to create this graph:

SO I tried to create some nodes and relationships with:

MERGE (D:POINT {NAME:'d'})<-[:LINKS]-(A:POINT {NAME:'a'})-[:LINKS]->(B:POINT {NAME:'b'})-[:LINKS]->(C:POINT {NAME:'c'})

But I cannot find out how I will create the relationships between D and B points also I cannot find out how I will link the A and C as well.

Do you have any Idea how to do that?

解决方案

In order to avoid unintentionally creating duplicate nodes and/or relationships, you must invoke MERGE on individual nodes and relationships.

To quote the dev manual:

When using MERGE on full patterns, the behavior is that either the whole pattern matches, or the whole pattern is created. MERGE will not partially use existing patterns — it’s all or nothing. If partial matches are needed, this can be accomplished by splitting a pattern up into multiple MERGE clauses.

For example, to properly create your graph without any duplicate nodes or relationships:

MERGE (A:POINT {NAME:'a'})
MERGE (B:POINT {NAME:'b'})
MERGE (C:POINT {NAME:'c'})
MERGE (D:POINT {NAME:'d'})
MERGE (A)-[:LINKS]->(B)
MERGE (A)-[:LINKS]->(C)
MERGE (A)-[:LINKS]->(D)
MERGE (B)-[:LINKS]->(C)
MERGE (D)-[:LINKS]->(B)

这篇关于Neo4j创建的图表明某些节点具有多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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