Neo4j:查找连接到多个2型节点的A型节点的网络

时间:2018-10-30 09:20:43

标签: neo4j

我的网络由类型A的节点组组成,它们之间具有多种关系(不同类型,但不重要ftm)。这些节点中的一些也可能与类型B的节点有关系。 现在,我正在寻找与多个类型B连接的类型A的节点组。 Neo4j甚至有可能吗?

1 个答案:

答案 0 :(得分:0)

示例模型

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

CREATE
  (a1:TypeA {name: 'A1'})-[:REFERS]->(a2:TypeA {name: 'A2'}),
  (a1)-[:REFERS]->(a3:TypeA {name: 'A3'}),
  (a2)-[:REFERS]->(a4:TypeA {name: 'A4'}),
  (a3)-[:REFERS]->(a4),
  (c1:TypeC {name: 'C1'})-[:REFERS]->(a1),
  (c1)-[:REFERS]->(a2),
  (d1:TypeD {name: 'D1'})-[:REFERS]->(a1),
  (d1)-[:REFERS]->(a3),
  (b1:TypeB {name: 'B1'})-[:REFERS]->(a3),
  (b1)-[:REFERS]->(a4),
  (b2:TypeB {name: 'B2'})-[:REFERS]->(a4),
  (e1:TypeE {name: 'E1'})-[:REFERS]->(a3),
  (e1)-[:REFERS]->(b1);

graphical representation of the example graph

解决方案

第二行检测到您的TypeA网络。基于此,第三行标识所有已连接的TypeB,它们在第5行中进行计数并在第6行中进行过滤。

MATCH
  networkA = (startNode:TypeA)-[:REFERS]-(:TypeA)-[:REFERS]-(:TypeA)-[:REFERS]-(:TypeA)-[:REFERS]-(:TypeA),
  (startNode)-[:REFERS]-(nodeB:TypeB)
WITH
  nodes( networkA) AS networkANodes, count(nodeB) AS nodeBAmount
WITH networkANodes WHERE nodeBAmount >= 2
RETURN
  networkANodes;

结果

╒══════════════════════════════════════════════════════════════════════╕
│"networkANodes"                                                       │
╞══════════════════════════════════════════════════════════════════════╡
│[{"name":"A4"},{"name":"A3"},{"name":"A1"},{"name":"A2"},{"name":"A4"}│
│]                                                                     │
├──────────────────────────────────────────────────────────────────────┤
│[{"name":"A4"},{"name":"A2"},{"name":"A1"},{"name":"A3"},{"name":"A4"}│
│]                                                                     │
└──────────────────────────────────────────────────────────────────────┘

graphical representation of the result