Reputation: 21
My graph is a directed tree an each branch along the tree has a unique label for each of the nodes. For example all the nodes in branch 1 have label:branch 1
and all the nodes in branch 2 have label:'branch 2'. The root node (node 0) has both labels: branch 1
:branch 2
What is the CYPHER query to list all the node IDs in branch 1
starting from the root node in sequence to the last node (using the label:'branch 1' to find the matching nodes). I.e., for each node listed, it and the node immediately before must also have label 'branch 1`.
Upvotes: 0
Views: 2229
Reputation: 9952
If I understand you right you're not actually using Neo4j 2.0 :Label
labels on your nodes, but a property called label
on your relationships? If so a general query could be something like
START root=node(0)
MATCH path=root<-[rels:IS_BEFORE*1..100]-leaf
WHERE ALL(rel in rels WHERE rel.label = "branch 1")
RETURN EXTRACT(n in nodes(path) | ID(n)) as nodeIdSequence
This is probably not very efficient since it matches all branches and only limits the result to the relevant branch afterwards. It would be more efficient to identify the branch by relationship type, something like (root)-[:NEXT_ON_BRANCH_1]->(branchNode)
. Alternatively you could do the match in two steps: 1) match the first node on each branch and find the right branch. 2) Now that you know you have the right branch, match the rest of it. You could try something like
START root=node(0)
MATCH root<-[r:IS_BEFORE]-branch
WHERE r.label = "branch 1"
WITH branch
MATCH path=branch<-[:IS_BEFORE*1..100]-leaf
RETURN EXTRACT(n in nodes(path) | ID(n)) as nodeIdSequence
If this is not what your model looks like, please share sample data at http://console.neo4j.org
(Depth limit *1..100
above is arbitrary, set it to whatever you want, but usually setting some limit is a good idea.)
Upvotes: 0