1

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
2
  • Could you please add details on your question ? Right now, it's difficult to say what're you're asking.
    – merours
    Jul 23, 2014 at 16:50
  • 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 it
    – freshf
    Jul 23, 2014 at 17:16

3 Answers 3

2

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
  • Hey thanks for your effort, but i think you understand me wrong. Imagine i am the node with the id 488. I want to know which of my friends have mutual friends.
    – freshf
    Jul 23, 2014 at 21:47
1

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

1
  • Hey, here you are console.neo4j.org/r/pl3m0g ... the target is to generate a recommendation of potential subscription for the node 488. The Return value must be in this case the Node with the Id: 210... the direction of the relationship is important i think because it isn´t a friendship ..
    – freshf
    Jul 24, 2014 at 9:56
0

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
2
  • i already commented below @ulkas answer .. look at that console console.neo4j.org/r/pl3m0g .. i want to get the node 210 because it is a common subscription of my(node 488) subscriptions which is not my subscription yet.. thx
    – freshf
    Jul 24, 2014 at 10:19
  • Well "to know which of my friends have mutual friends" is not the same as knowing those mutual friends. I'll add how to do that to my answer then.
    – jjaderberg
    Jul 24, 2014 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.