Rubyman
Rubyman

Reputation: 874

Cypher : multiple paths and conditions on path node properties joined by OR

Need help with cypher like following,

MATCH (user)
WHERE (ID(user) = 999)
MATCH (user)<-[rel1:`post`]-(p:`Post`)
WHERE ((p)-[r:`review`]->(w:Review) AND r.score IN [4,5] AND w.category='FB') OR
((p)-[r:`review`]->(w:Review) AND r.score IN [6,8] AND w.category='INSTA')
RETURN DISTINCT(p)

This gives undefined variable r error.

Upvotes: 0

Views: 91

Answers (2)

InverseFalcon
InverseFalcon

Reputation: 30397

Raj's approach is correct if there is only one relationship type here.

However you said "the relation after OR could be different", so you are looking for either one relationship type meeting a certain predicate or a different type with a different predicate.

You could use pattern comprehensions here to collect pieces of a pattern match here, and this will let you introduce new variables within the scope of the comprehension.

MATCH (user)
WHERE ID(user) = 999
MATCH (user)<-[:`post`]-(p:`Post`)
WITH user, p, [(p)-[r:`review`]->(w:Review) WHERE r.score IN [4,5] AND w.category='FB' | w] as midFB_Review, [(p)-[r:`review`]->(w:Review) WHERE r.score IN [6,8] AND w.category='INSTA' | w] as highINSTA_Review
WITH user, p
WHERE size(midFB_Review) + size(highINSTA_Review) > 0
RETURN p

Note your relationship type (or even the entire pattern you match to) can differ in the separate pattern comprehensions.

Upvotes: 2

Rajendra Kadam
Rajendra Kadam

Reputation: 4052

New variables (r and w, here) are not allowed in WHERE clause.

You can only check variables if it's present before WHERE.

You can get the same results by moving the relationship check from WHERE to MATCH Clause like:

MATCH (user) WHERE (ID(user) = 999) 
MATCH (user)<-[rel1:`post`]-(p:`Post`)-[r:`review`]->(w:Review) 
WHERE (r.score IN [4,5] AND w.category='FB') OR (r.score IN [6,8] AND w.category='INSTA') 
RETURN DISTINCT(p)

Upvotes: 1

Related Questions