Recommendations with Collaborative Filtering

Building on top of last Cypher tutorial on Movie Graph, we want to use the knowledge graph to build a recommendation engine. We want to find the second-degree actors in Tom Hank’s network. This will show us all the actors that Tom may not have worked with yet and we can recommend Tom who he wants to work with next. The overall query is as below. Let’s break it down.

MATCH (tom:Person {name: “Tom Hanks”})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(coActors:Person)-[:ACTED_IN]->(m2:Movie)<-[:ACTED_IN]-(cocoActors:Person)

WHERE tom <> cocoActors

AND NOT (tom)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(cocoActors)


The first line is to connect Tom Hanks to all the movies he has acted in, find out which other co-actors are in those movies, find out which other movies those co-actors have acted in and find out which other actors are in the co-actors’ movies. Overall, it’s Tom Hanks -> Tom Hank’s Movies -> Co-actors in Tom Hank’s Movies -> Co-actors’ Movies -> Co-actors in Co-actors’ Movies (cocoActors).

Once we have these information, we can find the connections between Tom Hanks and these cocoActors, where both Tom and the cocoActors have not been in the same movie and return the names of the cocoActors.

How to meet them?

You can find the most frequently appeared cocoActors in Tom’s network. One of the most frequents is Tom Cruise and we can now check how Tom Hanks is connected to Tom Cruise as follows:

MATCH (tom:Person {name: ‘Tom Hanks’})-[:ACTED_IN]->(movie1:Movie)<-[:ACTED_IN]-(coActor:Person)-[:ACTED_IN]->(movie2:Movie)<-[:ACTED_IN]-(cruise:Person {name: ‘Tom Cruise’})

WHERE NOT (tom)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(cruise)

RETURN tom, movie1, coActor, movie2, cruise

The two recommendations we can perform using this simple knowledge graph are who to meet and how to meet them! The power of knowledge graph!



Data Scientist

Leave a Reply