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:
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:
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:
-
Find an actor that acted in a Movie (
MATCH (a:Actor)-[r:ACTED_IN]→(m:Movie)
) -
Create (using
MERGE
) a Role node setting it’s name to the role in the ACTED_IN relationship. -
Create (using
MERGE
) the PLAYED relationship between the Actor and the Role nodes. -
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
You can use the following query to find all Actors with an :ACTED_IN
relationship to a Movie.
MATCH (a:Actor)-[r:ACTED_IN]->(m:Movie)
RETURN a, r, m
Append a MERGE
statement to find or create a Role node with a name
property derived from the :ACTED_IN
relationship.
Then MERGE
the :PLAYED
relationship from the Actor to the Role node, and an :IN_MOVIE
relationship from the Role node to the Movie.
Solution
Below is the statement required to merge a new Role node for each :ACTED_IN
relationship.
// 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)
This query should add 5 labels, create 5 nodes, set 5 properties, and create 10 relationships.
Click Run in Sandbox to execute the query and then click the Try again… button to verify that the query has succeeded.
If the numbers do not match up, try refreshing your browser to reset the data in your Sandbox instance.
Summary
In this challenge, you demonstrated that you can refactor the graph to add an intermediate node.
Congratulations! You have completed this course.