DEV Community

Cover image for 🚀Building a Course Recommendation System with Neo4j Aura🪷
AryanKumar1008
AryanKumar1008

Posted on

🚀Building a Course Recommendation System with Neo4j Aura🪷

Introduction

In today’s world, data is deeply interconnected. Traditional relational databases work well when you want to store structured rows and columns, but they struggle when you need to explore relationships. That’s where graph databases come in.

Neo4j is one of the most popular graph databases, and its free cloud service Neo4j Aura makes it simple for students and developers to get started. In this post, I’ll demonstrate how I used Neo4j Aura to build a university course recommendation system.

This project models students, courses, professors, and departments as nodes, and their connections as relationships. With just a few Cypher queries, we can recommend courses to students based on their peers’ enrollments.


Why Graph Databases?

Imagine you want to answer a question like:

“Which courses are taken by students similar to me?”

“Which professors are most connected to AI-related courses?”

In SQL, these queries would require multiple joins and be quite complex. In Neo4j, however, these queries are natural because the relationships are stored as first-class citizens in the database.

Graphs allow you to think in terms of connections, which is ideal for recommendation systems.


Data Model

For this project, I used four main types of nodes:

Student → Represents a university student

Course → Represents a subject or course

Professor → Represents a teacher who teaches a course

Department → Represents the academic department

Relationships include:

(:Student)-[:ENROLLED_IN]->(:Course)

(:Professor)-[:TEACHES]->(:Course)

(:Course)-[:PART_OF]->(:Department)

This simple schema is powerful enough to answer interesting queries.

Here’s a small example:

(Aryan:Student) -[:ENROLLED_IN]-> (AI Basics:Course) <-[:TEACHES]- (Dr. Rao:Professor)


Setting Up Neo4j Aura

  1. Create a free Neo4j AuraDB instance at aura.neo4j.io.

  2. Copy your database ID (mine is d813df50).

  3. Open Neo4j Browser and log in with your credentials.

  4. You’re now ready to run Cypher queries.


Creating Sample Data

Let’s add some sample data for students, courses, and professors:

// Create students
CREATE (:Student {name: "Aryan"});
CREATE (:Student {name: "Riya"});
CREATE (:Student {name: "Kunal"});

// Create courses
CREATE (:Course {name: "AI Basics"});
CREATE (:Course {name: "Data Structures"});
CREATE (:Course {name: "Machine Learning"});

// Create professors
CREATE (:Professor {name: "Dr. Rao"});
CREATE (:Professor {name: "Dr. Sharma"});

// Relationships
MATCH (s:Student {name:"Aryan"}), (c:Course {name:"AI Basics"})
CREATE (s)-[:ENROLLED_IN]->(c);

MATCH (s:Student {name:"Riya"}), (c:Course {name:"AI Basics"})
CREATE (s)-[:ENROLLED_IN]->(c);

MATCH (s:Student {name:"Riya"}), (c:Course {name:"Machine Learning"})
CREATE (s)-[:ENROLLED_IN]->(c);

MATCH (s:Student {name:"Kunal"}), (c:Course {name:"Data Structures"})
CREATE (s)-[:ENROLLED_IN]->(c);

MATCH (p:Professor {name:"Dr. Rao"}), (c:Course {name:"AI Basics"})
CREATE (p)-[:TEACHES]->(c);


Query 1: Find All Courses of a Student

MATCH (s:Student {name:"Aryan"})-[:ENROLLED_IN]->(c:Course)
RETURN c.name;

👉 This will return the list of courses that Aryan has enrolled in.


Query 2: Recommend Courses Based on Similar Students

This query recommends courses to Aryan that other students with similar enrollments are taking:

MATCH (s1:Student {name:"Aryan"})-[:ENROLLED_IN]->(c:Course)<-[:ENROLLED_IN]-(s2:Student)-[:ENROLLED_IN]->(rec:Course)
WHERE s1 <> s2
RETURN rec.name AS course, count(*) AS popularity
ORDER BY popularity DESC;

👉 If Aryan and Riya both take AI Basics, and Riya also takes Machine Learning, the system will recommend Machine Learning to Aryan.


Query 3: Find Professors Teaching AI-Related Courses

MATCH (p:Professor)-[:TEACHES]->(c:Course)
WHERE c.name CONTAINS "AI"
RETURN p.name, c.name;

👉 This query helps identify professors specializing in AI subjects.


Python Integration

Neo4j also provides a Python driver to connect your Aura instance to applications.

from neo4j import GraphDatabase

URI = "neo4j+s://d813df50.databases.neo4j.io"
AUTH = ("neo4j", "your-password-here")

driver = GraphDatabase.driver(URI, auth=AUTH)

def get_recommendations(student_name):
query = """
MATCH (s1:Student {name:$name})-[:ENROLLED_IN]->(c:Course)<-[:ENROLLED_IN]-(s2:Student)-[:ENROLLED_IN]->(rec:Course)
WHERE s1 <> s2
RETURN rec.name AS course, count(*) AS popularity
ORDER BY popularity DESC
"""
with driver.session() as session:
results = session.run(query, name=student_name)
for record in results:
print(record["course"], "-", record["popularity"])

get_recommendations("Aryan")
driver.close()

👉 This script prints recommended courses for Aryan directly in Python


Results

When I ran these queries in Neo4j Aura, I could see the relationships clearly in the graph visualization. The recommendation query successfully suggested Machine Learning for Aryan, since his classmate Riya had also enrolled in it.

Conclusion

This small project showed how Neo4j can be used to build a recommendation system with very little effort. By modeling students, courses, and professors as a graph, queries that would be complex in SQL become natural and elegant.

Neo4j Aura provides a simple and free way to experiment with graph databases. My next step will be to expand the dataset and explore Graph Data Science (GDS) algorithms like PageRank and community detection for deeper insights.

Top comments (0)