Showing results for 
Search instead for 
Did you mean: 

Cypher from a start node discover all relations

Graph Buddy


I have the graph below :

I want to start from the node 'zep3' and discover all relations until end nodes.
My need is from a start node, see the impacted relations of type FLUX (red color in the graph).

Any help is welcome,
Thank you.


This is best done with APOC path expander procedures, since we can change the traversal uniqueness behavior when expanding.

We can first use apoc.path.subgraphNodes() to get the distinct nodes reachable from your starting node, then we can MATCH out to just the :FLUX relationships:

You haven't shown what types of node these are or what the property keys are so I'll be making some up for this example:

MATCH (start:GreenNode {name:'zep3'})
CALL apoc.path.subgraphNodes(start, {}) YIELD node
MATCH (node)-[rel:FLUX]->()

Thank you for your response, i have a question,
I try to add an end node to the query like this:

MATCH (start:VM {name:'zep3'}),(end:APPLI {name: 'APPLI_A'})
CALL apoc.path.subgraphNodes(start, {endNodes:end}) YIELD node

but i have just the end node as a result.
How can i return all nodes between the start and end nodes ?

That's a different kind of thing, and it depends on the shape of your graph. apoc.path.spanningTree() is like subgraphNodes, but you get a path along with it, but it means that you only get ONE path to each node. If there are alternate paths, they will not be returned, and that may miss out on nodes if you want all nodes across all possible paths to all end nodes. However, in a tree structure or acyclic graph that's fine, you wouldn't miss out on anything.

If it's not a tree or acyclic graph, then you need to use apoc.path.expandConfig(), since we need a uniqueness behavior that will get you all possible paths to your end nodes.

In both cases you have YIELD path instead of YIELD node, so you'll need to do the job of UNWINDing the nodes in the paths and getting distinct nodes before matching further.

Also, if you want traversal to stop when you reach an end node, then use terminatorNodes instead of endNodes.

An example of usage:

MATCH (start:GreenNode {name:'zep3'}), (end:APPLI {name: 'APPLI_A'})
CALL apoc.path.spanningTree(start, {terminatorNodes:[end]}) YIELD path
UNWIND nodes(path) as node
MATCH (node)-[rel:FLUX]->()

Graph Buddy

Yes i want all nodes so i try this :

MATCH (start:VM {name:'zep1'}), (end:APPLI {name: 'APPLI_A'})
CALL apoc.path.expandConfig(start, {terminatorNodes:[end]}) YIELD path

but the request never ends ?

You may want to add a maxLevel value in the config properties. It looks like you may have an interconnected graph, which is a bad fit for an unlimited expansion like this, especially if you don't know if terminator nodes actually exist to constrain the expansion. Remember, Cypher by default (and here too since expandConfig() is using Cypher's default traversal uniqueness) is interested in finding ALL paths that match the pattern, and in this case, all possible paths of unlimited length. Consider the permutations of all possible paths in your graph if terminator nodes do not guard against unlimited expansion. A maxLevel may make sense.

Another option is to use apoc.path.subgraphAll(), and then filter the relationships list to the type that you want.

Nodes 2022
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.