0
votes

If I want to find a path of varibale length in my graph from node A to node B where all nodes in between have a certain label, and A and B have a different label -- how would I do that?

MATCH (A)-[*0..]->(B) WHERE ??

1

1 Answers

4
votes

This query should work if all nodes have only a single label:

MATCH p=(a)-[*]->(b)
WITH p, LABELS(a) AS la, LABELS(b) AS lb, NODES(p)[1..-1] AS nodes
WITH p, la, lb, nodes, LABELS(nodes[0]) AS label
WHERE label <> la AND label <> lb AND ALL(x IN nodes[1..] WHERE LABELS(x) = label)
RETURN p;

The query requires all the middle nodes to have the same label, and the end nodes to have a different label (or labels) than the middle nodes.