Reputation: 3173
Assuming I have two nodes and want to find a path between them, I can use the following cypher.
MATCH(a{ActivityId:"abc"}),(b{ActivityId:"def"}),p=((a)-[x *..15]->(b))
return p
If I wish to restrict the paths to those with certain attributes e.g. x.special=true
, how would I achieve this?
Specifically, this query gives me results (without the max-length restriction)
MATCH(a{ActivityId:"A3040", ProjectId:"AB.BC"}),(b{ActivityId:"HNP-L0-CON2-A1190",ProjectId:"AB.BC"}),p=((a)-[*{Critical:true}]->(b))
RETURN p
Whilst this does not
MATCH(a{ActivityId:"A3040", ProjectId:"AB.BC"}),(b{ActivityId:"HNP-L0-CON2-A1190",ProjectId:"AB.BC"}),p=((a)-[xs*..15]->(b))
WHERE all(x IN xs WHERE x.Critical)
RETURN p
Upvotes: 1
Views: 77
Reputation: 66989
These 3 approaches should all work:
MATCH p=((a {ActivityId: "A3040", ProjectId:"AB.BC"})-[*..15 {Critical: true}]->(b {ActivityId:"HNP-L0-CON2-A1190", ProjectId:"AB.BC"}))
RETURN p
MATCH p=((a {ActivityId: "A3040", ProjectId:"AB.BC"})-[*..15]->(b {ActivityId:"HNP-L0-CON2-A1190", ProjectId:"AB.BC"}))
WHERE ALL(r IN RELATIONSHIPS(p) WHERE r.Critical)
RETURN p
MATCH p=((a {ActivityId: "A3040", ProjectId:"AB.BC"})-[xs *..15]->(b {ActivityId:"HNP-L0-CON2-A1190", ProjectId:"AB.BC"}))
WHERE ALL(r IN xs WHERE r.Critical)
RETURN p
Upvotes: 3
Reputation: 816
You can use length
function to filter the results.
length() returns the length of a path.
MATCH (a{ActivityId:"A3040", ProjectId:"AB.BC"})
,(b{ActivityId:"HNP-L0-CON2-A1190",ProjectId:"AB.BC"})
,p=((a)-[*{Critical:true}]->(b))
WHERE length(p) < 15
RETURN p
Upvotes: 1
Reputation: 5047
You can use the all
predicate on the relationships:
MATCH p=((a {ActivityId: "abc"})-[*..15]->(b {ActivityId:"def"}))
WHERE all(n IN nodes(p) WHERE n.Critical)
RETURN p
Upvotes: 2