0
votes

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).

1
You might want to look over your question and fix the queries, the create query is broken and the match query uses different labels; and maybe exclude properties that are not relevant to the question. If you share a working model, with queries or as a console or graph gist, you're more likely to get fast and accurate answers.jjaderberg

1 Answers

3
votes

You can return path-expressions at any time.

MATCH path = (root:root:main)-[:HEAD]->(a:point)-[n:NEXT*]->(z:point)<-[:TAIL]-(root) 
RETURN extract(x in nodes(path) | [x, x-[:BRANCH]->()]), n

This x-[:BRANCH]->() returns a collection of paths, so if you just want to access the relationship you'd have to do

[p in x-[:BRANCH]->() | head(rels(p)) ]

For an example of how to implement an activity stream as an unmanaged extension you might have a look at this: https://github.com/jexp/neo4j-activity-stream