1
votes

I'm using cypher query to find the paths between two nodes using cypher query

Match p =  (n{ConceptID: 'C0000039'})-[r]-()-[s]- (m) 
WHERE r.RelationLabel CONTAINS "may_be_treat" or s.RelationLabel CONTAINS "may_be_treat"
RETURN p

But this query can return path with maximum depth of 2, how can I give path as variable(2,3,4,5,6) in this query, with condition as r.RelationLabel CONTAINS "may_be_treat"

2
I think you can use the variable length notation for that: (n)-[*1..5]->(m): find all n and m nodes connected with a path length no more than 5. I can recommend to use the cheatsheet.MarcoL
@MarcoL but then this query does not include the filter condition, which is what I requiregaurav1207
r should be connected directly to n and s directly connected to m, right?Bruno Peres
I just used "s" for creating a path length of 2, because . Ideally I don't require "s"gaurav1207
All relationships between n and n1 should have a property RelationLabel that contains "may_be_treat"? Or ony one is enough? I believe that is a good ideia edit the question and put on it a sample data set and the desired output.Bruno Peres

2 Answers

3
votes

You can use the variable-length pattern matching.

If you need that all relationships between n and n1 have a property called RelationLabel that CONTAINS the value "may_be_treat", then you can use the ALL function in conjunction.

Match p =  (n{ConceptID: 'C0000039'})-[*]-(m) 
WHERE ALL (r IN relationships(p) WHERE r.RelationLabel CONTAINS "may_be_treat")
RETURN p

If only one relationship with RelationLabel that contais "may_be_treat" is enough, then you can use ANY function instead.

Match p =  (n{ConceptID: 'C0000039'})-[*]-(m) 
WHERE ANY (r IN relationships(p) WHERE r.RelationLabel CONTAINS "may_be_treat")
RETURN p
1
votes

@Bruno Peres' answer is right.
To build on top of that if you want to have control over the length of the path you can either add an explicit variable-length pattern matching

Match p =  (n{ConceptID: 'C0000039'})-[*1..5]-(m) 
WHERE ANY (r IN relationships(p) WHERE r.RelationLabel CONTAINS "may_be_treat")
RETURN p

or use the length Path function.

Match p =  (n{ConceptID: 'C0000039'})-[*]-(m) 
WHERE length(p) < 5 AND ANY (r IN relationships(p) WHERE r.RelationLabel CONTAINS "may_be_treat")
RETURN p