How i find neo4j cypher?

Viewed 40

enter image description here

I'm trying to implement Friend Recommend.

Its starts from Node"You". And, I want to find Node"Anna".

In other words, It's a person whom two or more of my friends know in common. And example node of above is Anna.

I'd appreciate if your help.

node: Person

relationship: Friendship

MATCH (neo:Person {name: "You"})-[*2]->(target:Person)
MATCH (neo)-[:FRIENDSHIP]->(friend:Person)
MATCH (target)-[rel:FRIENDSHIP]->(friend)
WHERE target.name <> "You"
RETURN rel
1 Answers

I assume you only need a Person node that would like to recommend. Here is how you can proceed in 2 steps:

Step-1. Find Persons (friend-of-friend) and count of common friends:

MATCH path=(neo:Person {name: "You"})-[:RELATED_TO*2]->(target:Person) 
WHERE target <> neo
WITH neo, target, count(path) as cf 
RETURN target, cf

Step-2. Filter Persons who have 2 or more friends in common:

MATCH path=(neo:Person {name: "You"})-[:RELATED_TO*2]->(target:Person) 
WHERE target <> neo
WITH neo, target, count(path) as cf 
WITH target WHERE cf >= 2
RETURN target, cf
Related