Neo4j path from first child to last child


(Fsportelli0) #1

Hello. I have the following piece of graph:

As you can see, the red nodes are siblings connected by relationships, NEXT and NEXT_SIBLING; moreover, the first and last child are connected to the father by FIRST_CHILD_OF and LAST_CHILD_OF.
My goal is to find a way to store into a string "A B C D" with the right order.
This is an instance of the problem, because I cannot predict the number of children, that in this case are 4.

What I tried in the RETURN is the following:

apoc.text.join( (COLLECT(TRIM(words.text))), " " )

where words is a node. Unfortunately, the words are not ordered, probably I need to take advantage of the edges in some way.
Could you please suggest a solution?
Thanks.


(Dmiranda2791) #2

Maybe sorting children by the path length.

match path=(father: Father)-[:FIRST_CHILD_OF]-()-[:NEXT_SIBLING*0..]-(children)
with children.text as text
order by length(path)
with apoc.text.join(collect(trim(text)), " " ) as text
return text