DEV Community

Maheswar Sahoo
Maheswar Sahoo

Posted on

Building a Movie Recommendation Graph in Neo4j

Introduction

We’ve all been there: scrolling continuously through a streaming platform, unsure what to watch next. Traditional databases struggle to power personalized recommendations because they are optimized for rows and tables, not relationships. This is where graph databases like Neo4j comes into play.

In this post, I’ll Walk you through how to build a simple movie recommendation engine using Neo4j’s built-in Movie dataset. By the end, you’ll be able to:

  1. Model people, movies, and their relationships as a graph.
  2. Run Cypher queries to recommend movies based on shared preferences.
  3. Visualize results and understand why graph databases are a natural fit for recommendation systems.

I wrote this blog for beginners who are curious about using Neo4j to solve real-world problems.

Prerequisites

  1. Before you start, make sure you have Neo4j Desktop installed on your device.
  2. Basic familiarity with Cypher queries (MATCH, RETURN).
  3. The Movie dataset, which comes preloaded with most Neo4j instances.

Designing the Movie Graph

The dataset models two main entities:

Nodes:

  • Person (actors, directors, users)
  • Movie (films with title, released year, tagline)

Relationships:

  • :ACTED_IN (Person → Movie)
  • :DIRECTED (Person → Movie)
  • :REVIEWED (Person → Movie, with a rating)

Here's the Diagram:

Loading the Dataset

If you’re using Neo4j Browser, you can load the sample Movie dataset with:

:play movies

Follow the instructions to populate your graph with movies, actors, and reviews.

Querying the Graph

1. Find Movies by Actor
Let’s start simple: find all movies Tom Hanks acted in.

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(m:Movie)
RETURN m.title, m.released

2. Find Co-Actors
Who worked alongside Tom Hanks?

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(coActor)
RETURN coActor.name, collect(m.title) AS movies

3. Recommend Movies Based on Reviews
Here’s the fun part: recommending movies. Let’s recommend movies to Jessica Thompson based on what people with similar tastes liked.
MATCH (user:Person {name: "Jessica Thompson"})-[:REVIEWED]->(m:Movie)<-[:REVIEWED]-(other:Person)-[:REVIEWED]->(rec:Movie)
WHERE NOT (user)-[:REVIEWED]->(rec)
RETURN rec.title AS recommendation, count(*) AS score
ORDER BY score DESC
LIMIT 5

This query works by:

  1. Finding what Jessica reviewed.
  2. Looking for other people who reviewed the same movies.
  3. Recommending new movies those people also liked.

Visualizing the Graph

One of the strengths of Neo4j is visualization. When you run the queries in Neo4j Browser, you’ll see colorful nodes and relationships, making it easy to understand why a recommendation was made.

Troubleshooting Tips

  • Empty results? Double-check names (they are case-sensitive).
  • Slow queries? Add indexes on :Person(name) and :Movie(title) .
  • Dataset not loading? Make sure you ran :play movies and followed the load steps.

Conclusion

Recommendation systems are everywhere, from Netflix to Spotify and graphs provide a natural way to model them. With just a few Cypher queries, we can build a basic movie recommender that feels intuitive and easy to extend.

Next steps:

  • Add your own user nodes and ratings.
  • Try integrating Neo4j with Python or JavaScript for a full-stack app.
  • Explore more advanced algorithms with the Neo4j Graph Data Science Library.

By combining relationships, ratings, and recommendations, Neo4j makes it simple to answer the age-old question: What movie should I watch tonight?

Top comments (0)