0
votes

How can I write reflexive relationship query using Cypher? For example, I have defined a directional relationship named follows which is used between User nodes. What I am looking for is finding the users that follow each other.

Here is what I have tried:

MATCH (x:User)-[:FOLLOWS]->(y:User), y-[:FOLLOWS]->x

Detail regarding Neo4j:

Version: 3.5.2

Edition: Community

1

1 Answers

0
votes

The terminology for this is a relationship, not a property, though yes you can look for users who follow each other. Something like:

MATCH (x:User)-[:FOLLOWS]->(y:User)
WHERE id(x) < id(y) AND (y)-[:FOLLOWS]->(x)
RETURN x, y

The id predicate here is to ensure you only see each pairing once, and not an additional time for the same pairing in opposite order.