Jon Bates
Jon Bates

Reputation: 3173

Return a graph of a certain type of relationship (with a max-search-depth restriction)

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

Answers (3)

cybersam
cybersam

Reputation: 66989

These 3 approaches should all work:

  1. MATCH p=((a {ActivityId: "A3040", ProjectId:"AB.BC"})-[*..15 {Critical: true}]->(b {ActivityId:"HNP-L0-CON2-A1190", ProjectId:"AB.BC"}))
    RETURN p
    
  2. 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
    
  3. 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

Diego
Diego

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

Gabor Szarnyas
Gabor Szarnyas

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

Related Questions