2
votes

I have a simple graph with nodes labeled as Organization and two directed relationship types 'Control' and 'Influence'. My objective is -

Step-1 Given a node, find all nodes connected to it with 'Control' relationship (in any direction)

Step-2 For all nodes found by Step-1, find any outbound 'Influence' relationships (of any length) and include those nodes as well

What I have been able to come up thus far:

MATCH (x:Organization {ORGID: "5621"})-[:Control*1..]-(y) WITH y MATCH y-[:Influence*0..]-(z) RETURN y,z;

Questions
1) This query does not include the starting node, how can I get that in the result?

2) Ideally I'd like to get the relationships in the result also, it just returns the nodes

TIA

1

1 Answers

1
votes

Here is one way to do what you want:

MATCH (x:Organization { ORGID: "5621" }), p1 = x-[:Influence*0..]->(z)
WHERE NOT z-[:Influence]->()
WITH x, COLLECT(p1) AS c1
OPTIONAL MATCH x-[:Control*]-(y), p2=y-[:Influence*0..]->(z)
WHERE NOT z-[:Influence]->()
RETURN c1 + COLLECT(p2) AS result;

The query returns a collection of matching Influence paths. The WHERE clauses are used to make sure that each path is as long as possible (to avoid a lot of duplicate subpaths from showing up in the results). Every path is a collection of node(s) separated by relationships, and can consist of just a node if the path has no relationships.