How to get relationships of a particular type between nodes that are matched by a variable

I am not sure if I am explaining this in a good way but imagine a query of type:

MATCH (p:Person)-[r]-(m:Movie {title: "Awesomest Movie"}) WHERE type(r) <> "DIRECTED" RETURN p

This will return people associated with a particular movie. So far so good...

Say I want to then get family relationships between these people as well. In other words I need to be able to reach the relationships between the variable p refers to, and also filter these relationships to return the ones that are of interest, something like [r in relationships(p) | r WHERE type(r) IN ["TYPE_A", "TYPE_B"]]

You can do your initial query like this:

MATCH (p:Person)-[r:DIRECTED]-(m:Movie {title: "Awesomest Movie"})
RETURN p

And then if you want to find family rels as well, you could do this:

MATCH (p:Person)-[r:DIRECTED]-(m:Movie {title: "Awesomest Movie"}),
             (p)-[:TYPE_A|TYPE_B]->(other)
RETURN *

That will find rels of TYPE_A or TYPE_B

Well, the problem is that I want all relationships except DIRECTED, so I need a WHERE clause after first match. Also if I am not mistaken

MATCH (p:Person)-[r:DIRECTED]-(m:Movie {title: "Awesomest Movie"}),
             (p)-[:TYPE_A|TYPE_B]->(other)
RETURN *

would match all nodes that have TYPE_A or TYPE_B relationship with (p). In my case I only want to grab the relationships in between the nodes that are captured with (p)

Hello @ukirik :slight_smile:

MATCH (p:Person)-[r:DIRECTED]-(m:Movie {title: "Awesomest Movie"})
WITH p
MATCH (p)-[:TYPE_A|TYPE_B]->(other)
RETURN *

And if you want to use parameters:

MATCH (p:Person)-[r:DIRECTED]-(m:Movie {title: "Awesomest Movie"})
WITH p
MATCH (p)-[r]->(other)
WHERE type(r) IN ["TYPE_A", "TYPE_B"]
RETURN *

Regards,
Cobra

Hi! :slight_smile:

Thank you very much for the suggestion, but in this query wouldn't (other) not match nodes that are not captured in (p) as well? I realise the example is a silly one, in my actual case, the (p) would match approx 20-30 nodes, most (but not all) have some relationships with each other, which are the ones I am trying to capture.

Do you have an example please? :slight_smile:

Regards,
Cobra

Well I cant really dig into the details of the data or the schema, but I think the following graph should serve as an adequate simplification of what I am trying to do here..

Untitled_Diagram

So we have a node of type X, that is interesting for the query. We look for nodes of type Y that have a particular type of relationship (actually it's more like all types of relationships except one or two) with node X.

So far so good, the snippet in OP achieves that without any issues. Now if I want to capture the interactions between Y_1... Y_i (blue circles) but not the other Ys that are not connected to X (red circles). The issue is that there are a lot of different types of relationships between nodes of type Y and I only want to capture some of them (blue and green dashed lines) and avoid retrieving others (red dashed line). I should also mention there are plenty of connections between the blue nodes, and the red ones, which is why the suggestions above do not help, since I end up getting many other nodes that have no connection to X.

Does that explanation make sense?

Thanks in advance for your help and patience

Hello @ukirik :slight_smile:

Did you try a match like this?

MATCH (y1)-[r1]->(x)<-[r2]-(y2)
WHERE type(r1) IN ["TYPE_A", "TYPE_B"]
AND type(r2) IN ["TYPE_A", "TYPE_B"]
RETURN *

Regards,
Cobra