使用Cypher查找Neo4j中条件对之间的所有匹配项



我们需要为特定节点的每种类型的公共源创建一个报告,以解释:考虑节点的类型为 node.type = ["Apple","Windows","Linux","Other"] 以及以下形式的关系

(来源:ANode(-[r:ConnectedTo]->(target:ANode(

因此,每种类型的来源将是:

match (source:ANode)-[r:ConnectedTo]->(target:ANode)
return distinct target.type, source.name

我们需要为每个成对的类型组合确定重复的来源,因此,"Apple"和"Windows"之间,"Windows"和"Linux"之间的重复源,等等。

我已经尝试了以下内容,但它似乎仅适用于第一对组合:

match (source:ANode)-[r:ConnectedTo]->(target:ANode)
where target.type="Apple"
return distinct target.type, source.name
UNION
match (source:ANode)-[r:ConnectedTo]->(target:ANode)
where target.type="Windows"
return distinct target.type, source.name

我已经可以使用以下代码确定任何两个系统之间的公共源:

Match (node1:ANode)<-[:ConnectsTo]-(src)-[:ConnectsTo]->(node2:ANo‌​de) 
where ID(node1)<ID(node2) 
return node1.type,node1.name,node2.type,node2.name,src.name 

我看不出如何从这个到最终目标。

我认为这不是正确的方法,因为类型的数量可能远远超过 2-3 种。请帮助采用更优雅的方法

[已编辑]

可以使用聚合获取每个源/目标类型对的不同源名称的集合:

MATCH (source:ANode)-[r:ConnectedTo]->(target:ANode)
RETURN
  source.type AS sourceType,
  target.type AS targetType,
  COLLECT(DISTINCT source.name) AS sourceNames;

最新更新