Assume, paths are not of fixed length and can get very long.
I would like to retrieve all paths starting in node 'a' but having only the first node labeled 'B'. I also don't want all the subsequent nodes which would have been connected only through one of the nodes labeled with 'B' if it's not the first.
In the example graph, I want this to be the result of my query:
I had following idea how to approach this but got stuck on the filtering part of it:
MATCH p = (a:A)-[*]->(t)
WITH labels(t) as ls, a, t
//WHERE path has only first node labeled 'B'
RETURN a, t
In case the individual does not respond, I will try to present a solution. I am going to assume the requirement is to find the paths starting from a specific 'A' node, whose next node is of type 'B', and all remaining nodes along the path are not of type 'B'. The following query should provide that:
match p=(a:A {name: 'a'})-[:TO]->(:B)-[:TO*0..]->(n)
where none(i in nodes(p)[2..] where i:B)
return a, n, length(p) as length
If this does not answer your question, can your provide you specific requirement?
You can try this using pure cypher. You can also try APOC path methods, where you can specifiy the node labels of terminating nodes in the configuration, which would be the list of labels starting with 'A'
match(n:A1{id:0})
match p=(n)-[*]-(m)
with p, nodes(p)[1..-1] as notANodes, nodes(p)[-1] as aNode
where none(x in notANodes where 'A' in [y in labels(x) | substring(y, 0, 1)])
and 'A' in [y in labels(aNode) | substring(y, 0, 1)]
return p