Neo4j跨节点的“展平/折叠”关系

时间:2018-11-08 15:45:29

标签: neo4j cypher

我有一个图,如下图所示,灰色节点是事件。黄色节点是组件。组件取决于事件,而组件可以产生其他事件。不幸的是,节点标签没有清楚地显示出来,所以我加了数字

enter image description here

我希望能够编写一个仅返回事件(灰色圆圈)但保留事件之间的关系或实际依赖关系的查询。

最终结果应该是另一个看起来像这样的图:

enter image description here

这应该适用于任意大尺寸的图,屏幕右侧可能会有更多节点

用户ThirstForKnowledge已创建了用于导入图形的密码。在此处粘贴可见性

CREATE
  (c1:Component {name: 'Comp 1'})-[:PRODUCES]->(e3:Event {name: 'Event 3'}),
  (c1)-[:PRODUCES]->(e4:Event {name: 'Event 4'}),
  (c1)-[:DEPENDS_ON]->(e2:Event {name: 'Event 2'}),
  (c1)-[:DEPENDS_ON]->(e1:Event {name: 'Event 1'}),
  (c2:Component {name: 'Comp 2'})-[:DEPENDS_ON]->(e3),
  (c3:Component {name: 'Comp 3'})-[:DEPENDS_ON]->(e3),
  (c3)-[:DEPENDS_ON]->(e4),
  (e5:Event {name: 'Event 5'})<-[:PRODUCES]-(c2);

1 个答案:

答案 0 :(得分:4)

您有这样的想法吗?

创建模型

为便于进一步解答和解决方案,我注意到了我的图形创建语句:

CREATE
  (c1:Component {name: 'Comp 1'})-[:PRODUCES]->(e3:Event {name: 'Event 3'}),
  (c1)-[:PRODUCES]->(e4:Event {name: 'Event 4'}),
  (c1)-[:DEPENDS_ON]->(e2:Event {name: 'Event 2'}),
  (c1)-[:DEPENDS_ON]->(e1:Event {name: 'Event 1'}),
  (c2:Component {name: 'Comp 2'})-[:DEPENDS_ON]->(e3),
  (c3:Component {name: 'Comp 3'})-[:DEPENDS_ON]->(e3),
  (c3)-[:DEPENDS_ON]->(e4),
  (e5:Event {name: 'Event 5'})<-[:PRODUCES]-(c2);

graph

密码声明

MATCH
  path = (event1:Event)<-[:PRODUCES]-(:Component)-[:DEPENDS_ON]->(event2:Event)
RETURN
  event1.name AS eventUnderInspection, path;

结果

╒══════════════════════╤═════════════════════════════════════════════════════════════════════════════════╕
│"eventUnderInspection"│"path"                                                                           │
╞══════════════════════╪═════════════════════════════════════════════════════════════════════════════════╡
│"Event 4"             │[{"name":"Event 4"},{},{"name":"Comp 1"},{"name":"Comp 1"},{},{"name":"Event 1"}]│
├──────────────────────┼─────────────────────────────────────────────────────────────────────────────────┤
│"Event 4"             │[{"name":"Event 4"},{},{"name":"Comp 1"},{"name":"Comp 1"},{},{"name":"Event 2"}]│
├──────────────────────┼─────────────────────────────────────────────────────────────────────────────────┤
│"Event 3"             │[{"name":"Event 3"},{},{"name":"Comp 1"},{"name":"Comp 1"},{},{"name":"Event 1"}]│
├──────────────────────┼─────────────────────────────────────────────────────────────────────────────────┤
│"Event 3"             │[{"name":"Event 3"},{},{"name":"Comp 1"},{"name":"Comp 1"},{},{"name":"Event 2"}]│
├──────────────────────┼─────────────────────────────────────────────────────────────────────────────────┤
│"Event 5"             │[{"name":"Event 5"},{},{"name":"Comp 2"},{"name":"Comp 2"},{},{"name":"Event 3"}]│
└──────────────────────┴─────────────────────────────────────────────────────────────────────────────────┘

更新以满足新要求

解决方案

以下Cypher查询通过依赖事件之间的直接(扁平化)关系来丰富现有图形。

MATCH
  (sourceEvent:Event)<-[:DEPENDS_ON]-(:Component)-[:PRODUCES]->(targetEvent:Event)
MERGE (sourceEvent)<-[:FLATTENED_DEPENDS_ON]-(targetEvent);

执行上述查询后,您可以通过以下Cypher查询来呈现图形:

MATCH
  path = (:Event)<-[:FLATTENED_DEPENDS_ON]-(:Event)
RETURN
  path;

结果

graph2