简体   繁体   English

Cypher根据特定关系连接匹配的节点

[英]Cypher to connect matched nodes based on specific relationship

If I do: 如果我做:

MATCH (x:NODE {x.name: "Node1"})-[r:REL1]-(y:NODE) return x,r,y

How do I then find all the REL1 relationships amongst the set of x and y nodes? 然后,如何找到xy节点集中的所有REL1关系?

EDIT: 编辑:

Based on the answers I think the question wasn't clear. 根据答案,我认为问题尚不清楚。

Example graph: 图表示例:

create (:T1 {name:1}), (:T1 {name:2}), (:T1 {name:3}), (:T1 {name:4}), (:T1 {name:5}), (:T1 {name:6}), (:T1 {name:7})
match (a:T1 {name:1}), (b:T1 {name:2}) create (a)-[r:REL1]->(b)
match (a:T1 {name:1}), (b:T1 {name:3}) create (a)-[r:REL1]->(b)
match (a:T1 {name:5}), (b:T1 {name:4}) create (a)-[r:REL1]->(b)
match (a:T1 {name:5}), (b:T1 {name:3}) create (a)-[r:REL1]->(b)
match (a:T1 {name:5}), (b:T1 {name:2}) create (a)-[r:REL1]->(b)
match (a:T1 {name:5}), (b:T1 {name:1}) create (a)-[r:REL1]->(b)
match (a:T1 {name:7}), (b:T1 {name:6}) create (a)-[r:REL2]->(b)
match (a:T1 {name:7}), (b:T1 {name:5}) create (a)-[r:REL2]->(b)
match (a:T1 {name:7}), (b:T1 {name:4}) create (a)-[r:REL2]->(b)
match (a:T1 {name:7}), (b:T1 {name:3}) create (a)-[r:REL2]->(b)

I'm going to find everything that has a REL1 relationship with node name:5 . 我将查找与节点name:5具有REL1关系的所有内容。 I want to also find all the REL1 relationships amongst the returned nodes. 我还想找到返回节点之间的所有REL1关系。

So I should get 所以我应该得到

5->4
5->3
5->2
5->1
1->2
1->3

But not 但不是

7->5
7->4
7->3

Because those are REL2 relationships. 因为那些是REL2关系。

So I think I can do it like this: 所以我想我可以这样做:

match (a:T1 {name: 5})-[b:REL1]->(c:T1) return a,b,c
union match (a:T1)-[b:REL1]-(c:T1) return a,b,c

But the problem is that the graph I'm working with is really large, so this seems quite inefficient. 但是问题在于我正在使用的图形非常大,因此效率似乎很低。 I'd prefer to be able to 我希望能够

  1. Select a set of nodes (everything connected to 5 ) 选择一组节点(所有连接到5节点)
  2. Find all the connections amongst those nodes 找到这些节点之间的所有连接

如果需要找出x和y节点集中的所有REL1关系,则从x节点中删除条件。

MATCH (x:NODE)-[r:REL1]-(y:User) return x,r,y

我们正确地使用双向,以任何一种方式查找关系,但是当您尝试查找所有关系时,您仅选择一个关系REL1,从关系[]中删除标签,因此可以使用以下查询并返回*,它将返回所有使用的变量。

MATCH (x:NODE {x.name: "Node1"})-[r]-(y:NODE) return *

To get all the distinct node pairs connected by one or more REL1 relationships, just do this: 要获得通过一个或多个REL1关系连接的所有不同的节点对,只需执行以下操作:

MATCH (a:T1)-[:REL1]->(c:T1)
RETURN DISTINCT a, c

The result would be: 结果将是:

╒══════════╤══════════╕
│"a"       │"c"       │
╞══════════╪══════════╡
│{"name":1}│{"name":2}│
├──────────┼──────────┤
│{"name":1}│{"name":3}│
├──────────┼──────────┤
│{"name":5}│{"name":1}│
├──────────┼──────────┤
│{"name":5}│{"name":2}│
├──────────┼──────────┤
│{"name":5}│{"name":3}│
├──────────┼──────────┤
│{"name":5}│{"name":4}│
└──────────┴──────────┘

If you also want the relationship(s) between each pair, you can do this: 如果您还希望每对之间的关​​系,可以执行以下操作:

MATCH (a:T1)-[r:REL1]->(c:T1)
RETURN a, c, COLLECT(r) AS rels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM