Second level relationship direction

Hi everyone.. I have been working on cypher and Neo4j.. From the past few days, I was trying to figure out how to get the direction of nodes in the multiple depths of the graph.. I created a simple graph and following is the query I executed..

MATCH (n {entityName: "John"})-[r*1..2]-(p)
return n.entityName, collect(distinct(p.entityName)), [x in r | type(x)], [x in r | case (startNode(x) = n) when true then "outward" when false then "inward" end] as direction

The output correctly showed the direction of first level relationship but for the successive one's it always showed "inwards". Can someone help me with this and improve the results..
Assume all the nodes have same properties (entity name and entity id)..

Hi @gaurav.malhotra,
Can you try algo.shortestPath
shortest path

1 Like

Your criteria is such that if the startNode() of x (for each relationship in the path) is equal to n (your "John" entity) that it will emit "outward", otherwise it will emit "inward". I don't think this comparison to n is what you meant to do.

If you add a path variable, and use a range for the path length as the index, you can see if the node in that path at that index is the start node of the relationship at that index, and if so then it's an incoming relationship, else it's outgoing:

MATCH path = (n {entityName: "John"})-[*1..2]-(p)
WITH path, range(0, length(path) - 1) as index
RETURN [i in index | CASE WHEN nodes(path)[i] = startNode(relationships(path)[i]) THEN 'incoming' ELSE 'outgoing' END] as directions
1 Like