1
votes

I am new in Neo4j and Cypher and writing on my BA-Thesis in which I compare a RDBMS against Neo4j Graph Database in case of social networks. I´ve defined some queries in SQL and Cypher for a Performance Test over JDBC and REST API in JMETER. However, I have a problem declaring the Cypher query to get the Nodes which are the mutual friends of friends for a certain Node.

My first approach was like so:

MATCH (me:Enthusiast {Id: 488})-[:abonniert]->(f:Enthusiast)-[:abonniert]->(fof:Enthusiast)<-[:abonniert]-(f) RETURN o
3
Could you please add details on your question ? Right now, it's difficult to say what're you're asking.merours
If a NODE XY has two or more friends who have (partially) mutual friends then i want to return those information. So I don´t want to search mutual friends on the first degree but in the second degree. hope you understand itfreshf

3 Answers

2
votes

I guess you're pretty close with your Cypher statement. I assume that "mutual friend on 2nd degree" means that I'm mutual friend with someone the target is mutual friend as well?

If so (shortening labels and relationship types for readbility):

MATCH 
(me:En {Id: 488})-[:abonniert]->(f:En)-[:abonniert]->(fof:En),
(fof)-[:abonniert]->(f)-[:abonniert]->me
RETURN fof
1
votes

it would be nice if you can create an example scenario at http://console.neo4j.org/ . i would also omit the relationships direction.

MATCH (me:Enthusiast {Id: 488})-[:abonniert]->(f:Enthusiast),
(f)-[:abonniert]-(x:Enthusiast)-[:aboniert]-(y:Enthusiast)
WHERE f--y AND Id(y) <> 488
RETURN f, y, count(x) as NrMutFr

edit

try this console query, works for the scenario: http://console.neo4j.org/r/tws07k

my above query would in that case be

MATCH (me:Enthusiast {Id: 488})-[:abonniert]->(f:Enthusiast),
(f)-[:abonniert]->(x:Enthusiast)<-[:aboniert]-(y:Enthusiast)
WHERE me--y 
RETURN f, y, count(x) as NrMutFr

the difference between your posted question query is that you must finish the last node with a new substitute y and not f. than also, if necessary, again match that y with starting me node

0
votes

Once you've matched your friends you should be able to express the rest of the query as a path predicate: match "my friends", filter out everyone except "those of my friends who have some friend in common", which amounts to the same as "those of my friends who have a friend-of-friend who is a friend of mine.

MATCH (me:Enthusiast { Id: 488 })-[:abonniert]->(f)
WHERE f-[:abonniert]-()-[:abonniert]-()<-[:abonniert]-me
RETURN f

Here's a console: http://console.neo4j.org/r/87n0j9. If I have misunderstood your question you can make changes in that console, click "share" and post back the link here with an explanation of what result you expect to get back.

Edit
If you want to get the nodes that are two or more of your friends are related to in common, you can do

MATCH (me:Enthusiast { Id: 488 })-[:subscribed]->(f)-[:subscribed]->(common) 
WITH common, count(common) AS cnt 
WHERE cnt > 1 
RETURN common

A node that is a common neighbour of your neighbours can be described as a node you can reach on at least two paths. You can therefore match your neighbour-of-neighbours, count the times each "non" is matched, and if it is matched more than once then it is a "non" that is common to at least two of your neighbours. If you want you can return that count and order the result by it as a type of scoring (since this seems to be for recommendation purposes).

MATCH (me:Enthusiast { Id: 488 })-[:subscribed]->(f)-[:subscribed]->(common) 
WITH common, count(common) AS score 
WHERE score > 1 
RETURN common, score
ORDER BY score DESC