I'm implementing a something like a linked-list structure in a Neo4j graph. The graph is created by executing many statements similar to this:
CREATE (R1:root{edgeId:2})-[:HEAD]->
(:node{text: 'edge 2 head text', width:300})-[:NEXT{edge:2, hard:true}]->
(:node{text: 'edge 2 point 0'})-[:NEXT{edge:2}]->
(n0:node{text: 'edge 2 point 1'}),
(n0)-[:BRANCH]->(:root{edgeId:3}),
(n0)-[:NEXT{edge:2}]->
(:node{text: 'edge 2 point 2'})-[:NEXT{edge:2}]->
(:node{text: 'edge 2 point 3'})<-[:TAIL{edge:2}]->(R1)
Traversing an edge means starting with a root node, following its outgoing HEAD
relationship to the first node, and following the chain of NEXT
relationships until reaching a node with an incoming TAIL
relationship from the root we started from.
i.e.:
MATCH path = (root:root:main)-[:HEAD]->(a:point)-[n:NEXT*]->(z:point)<-[:TAIL]-(root)
RETURN nodes(path), n
Every node has an outgoing NEXT
relationship, but some nodes also have BRANCH
relationships, which point to the root nodes of other edges.
In the above query, nodes(path)
obviously returns all the nodes along the edge, and n
lists the outgoing NEXT
relationship for each node along it. How could I modify this query so that, in addition to the outgoing NEXT
relationship, it also returns any outgoing BRANCH
relationships
How can I modify the above query so that each record returned contains a node on the path along with a list of all outgoing relationships (both NEXT
and BRANCH
) from it?
Note that I don't want to traverse the BRANCH
edges in this query, I just want it to tell me they're there.
(PS I'm implementing this strategy in Java, but so far have preferred executing Cypher queries directly rather than using the Traversal API. If I'm making this more difficult on myself by doing so, please bring it to my attention).