Adding a Role Node

We want to infer more from the roles that an actor played in a movie. The same role could be repeated in multiple movies. Furthermore, we might want to add how different roles interact with each other in the same movie or between movies.

Given the current instance model:

Current instance model

How would you refactor the graph to add an intermediate node representing the role property of the ACTED_IN relationship?

Here is the proposed instance model:

Current instance model

We add a Role node using the role property from the ACTED_IN relationship.

In this instance model we also show that some Role nodes could be related to each other with the INTERACTED_WITH relationship, but you will not implement the INTERACTED_WITH relationships.

Write and run refactor code to:

  1. Find an actor that acted in a Movie (MATCH (a:Actor)-[r:ACTED_IN]→(m:Movie))

  2. Create (using MERGE) a Role node setting it’s name to the role in the ACTED_IN relationship.

  3. Create (using MERGE) the PLAYED relationship between the Actor and the Role nodes.

  4. Create (using MERGE) the IN_MOVIE relationship between the Role and the Movie nodes.

Your code should create 5 nodes and 10 relationships.

Validate Results

Once you have run the query, click the Check Database button and we will check the database for you.

Hint

This query is missing the correct relationship types to create the relationships between the Actor, Role, and Movie nodes.

cypher
MATCH (a:Actor)-[r:ACTED_IN]->(m:Movie)
MERGE (x:Role {name: r.role})
MERGE (a)-[:??????]->(x)
MERGE (x)-[:??????]->(m)

Solution

This Cypher will create a new Role node for each :ACTED_IN relationship:

cypher
Create Role nodes
// Find an actor that acted in a Movie
MATCH (a:Actor)-[r:ACTED_IN]->(m:Movie)

// Create a Role node
MERGE (x:Role {name: r.role})

// Create the PLAYED relationship
// relationship between the Actor and the Role nodes.
MERGE (a)-[:PLAYED]->(x)

// Create the IN_MOVIE relationship between
// the Role and the Movie nodes.
MERGE (x)-[:IN_MOVIE]->(m)

Summary

In this challenge, you demonstrated that you can refactor the graph to add an intermediate node.

Congratulations! You have completed this course.