cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to return from & to nodes from a path?

I want to return one row for each segment in a path. This is the Cypher code I've devised so far. There must be a better way.

  match ... --> (i:IMAGE)
  match p = (i)-[*]->(f:IMAGE) where NOT (f)-->(:IMAGE)
  with nodes(p) as q
  with [[q[0],q[1]], [q[1],q[2]], [q[2],q[3]], [q[3],q[4]], [q[4],q[5]], [q[5],q[6]]] as tuples
  unwind tuples as seg
  return seg[0].name as from, seg[1].name as to 

My biggest problem is that it only goes six levels deep. Another inconvenience is that it returns lots of null rows for shallow paths.

2 REPLIES 2

APOC Procedures can help here, notably the function apoc.coll.pairsMin(), which takes a list, such as [1,2,3,4,5], and breaks it down to a list of adjacent pairs, in this example case: [[1,2], [2,3], [3,4], [4,5]]

You can use this to get pairs of adjacent nodes in the list:

...
UNWIND apoc.coll.pairsMin(nodes(p)) as pair
RETURN pair[0].name as from, pair[1].name as to

Thank you. This is what I had hoped to find.