java – 如何为这种情况编写Cypher查询?

我有一个如下图所示的图.java  – 如何为这种情况编写Cypher查询?

WAR可以发布多个SERVICE,也可以DEPENDSON多个SERVICE.

我的要求是获得给定WAR的依赖WAR.

我知道如何编写查询以获得第一层依赖项.

MATCH (w:WAR)-[:PUBLISH]->(s:SERVICE)<-[:DEPENDSON]-(otherWar:WAR)

我的问题是如何获得给定WAR的多层依赖.

在这种情况下,在war1和war3之间,有两个PUBLISH / DEPENDSON关系.如何用可变长度关系描述这种关系?

解决方法:

[更新]

找到一场战争取决于另一场战争的方式

假设PUBLISH和DEPENDSON关系总是从WAR节点开始并以SERVICE节点结束,我相信这个查询将找到war3(最终)依赖war1的所有路径.

MATCH p=(w:WAR {name:'war1'})-[:PUBLISH]->
  ()-[:PUBLISH|DEPENDSON*0..]-()
  <-[:DEPENDSON]-(otherWar:WAR {name:'war3'})
RETURN p;

使用@GaborSzarnyas提供的相同示例数据,上面的查询产生以下结果:

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                                                          |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [Node[6]{name:"war1"},:PUBLISH[6] {},Node[9]{name:"s1"},:DEPENDSON[8] {},Node[7]{name:"war2"},:PUBLISH[10] {},Node[11]{name:"s3"},:DEPENDSON[11] {},Node[8]{name:"war3"}]  |
| [Node[6]{name:"war1"},:PUBLISH[7] {},Node[10]{name:"s2"},:DEPENDSON[9] {},Node[7]{name:"war2"},:PUBLISH[10] {},Node[11]{name:"s3"},:DEPENDSON[11] {},Node[8]{name:"war3"}] |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

寻找战争的依赖关系

此查询应查找依赖war1的所有不同WAR节点:

MATCH (w:WAR {name:'war1'})-[:PUBLISH]->()-[:PUBLISH|DEPENDSON*0..]-()<-[:DEPENDSON]-(otherWar:WAR)
RETURN DISTINCT otherWar;

结果是:

+----------------------+
| otherWar             |
+----------------------+
| Node[7]{name:"war2"} |
| Node[8]{name:"war3"} |
+----------------------+
上一篇:图数据库neo4j


下一篇:图形据库Neo4j从入门到精通(含D3.js+Neo4j完整实战项目)