0
votes

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?

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. I want to also find all the REL1 relationships amongst the returned nodes.

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.

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)
  2. Find all the connections amongst those nodes
3
x.name cannot be used that way, since a property name cannot contain a period -- unless you use back-ticks to quote the name. Did you mean to use name instead? And what do you mean by "amongst the set of x and y nodes"? Do you want to get all the REL1 relationships between each distinct x and y pair?cybersam
sorry about the x.name that was just a mistake.explodecomputer
@cybersam "Do you want to get all the REL1 relationships between each distinct x and y pair?" - yes. i think the use of union in my example is even wrong.explodecomputer

3 Answers

0
votes

We are correctly using Bi-Directional, to find the relationship either ways, but you are selecting only one relationship REL1, when you trying to find all the relationship remove the label from the relationship [], So you can use the below query and return *, it will return all the variables used.

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

To get all the distinct node pairs connected by one or more REL1 relationships, just do this:

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
0
votes

If you need to find out all the REL1 relationships amongst the set of x and y nodes then remove the condition from x node.

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