I have a cypher query that returns list of users that are recommended to a user to follow, but am getting duplicate results when the cypher is executed.
Here is the cypher query:
MATCH (user:User { id: $userId })
MATCH (user)-[interestRel:INTERESTED_IN]->()<-[:INTERESTED_IN]-(recommendedUsers)
WITH DISTINCT recommendedUsers, interestRel, user
WHERE NOT recommendedUsers = user AND
NOT exists((user)-[:FOLLOWING]->(recommendedUsers))
RETURN recommendedUsers {
.id,
following: false
} ORDER BY interestRel.interestLevel DESC SKIP $skip LIMIT $limit
I understand there will be duplicates because a user might be INTERESTED_IN
multiple nodes, so when the INTERESTED_IN
relationship is traversed, for each node that has a INTERESTED_IN
relationship, duplicate users will be returned. But am returning DISTINCT
users, so I don't understand why duplicate users are still returned.
I noticed that when the INTERESTED_IN
relationship is bound to a variable (interestRel
) which is used in the query, that's when duplicate results are returned.
How do I get rid of the duplicates and still reference the INTERESTED_IN
(interestRel
) relationship?