Reputation: 211
Is it possible to use patterns with a case statement in cypher ? specifically to return true or false if pattern exist. I am trying to model human relationships where friendship is not reciprocated.
So (user)-[:friend]->(user2) is a separate data point from (user2)-[:friend]->(user1)
Then I want to run a query that would show user1 who considers them their friends in return
something that would look like this (which doesn't work obviously)
MATCH (asker)-[:friend]->(target) RETURN CASE (target)-[:friend]->(asker)
WHEN present THEN true
ELSE THEN false As returnsFriendship
anyway to make that work ?
I know that a work around is to run a query looking for those who consider user1 friends and then loop through them outside cypher in my application but I was wondering if there is a pure cypher way to do this.
Upvotes: 0
Views: 230
Reputation: 66967
The CASE
clause is not the only way to get a boolean result, and is not necessary for your use case.
Patterns like (target)-[:teaches]->(asker)
produce a (possibly empty) collection of paths, so this would work:
MATCH (asker), (target)
RETURN asker, target,
SIZE((target)-[:teaches]->(asker)) > 0 AS returnsFriendship
But if only need to test for the existence of the pattern, using the EXISTS
function would be better:
MATCH (asker), (target)
RETURN asker, target,
EXISTS((target)-[:teaches]->(asker)) AS returnsFriendship
Upvotes: 2