In the previous Challenge, you eliminated duplication by taking the data in the languages property and creating Language nodes that are related to movies.
This challenge has three steps:
-
Modify and run the query in the sandbox query pane to use the data in the genres property for the Movie nodes and create Genre nodes using the IN_GENRE relationship to connect Movie nodes to Genre nodes.
-
Delete the genres property from the Movie nodes.
-
Rewrite the query for the use case: What drama movies did an actor act in?
Copy this query to the sandbox query pane, rewrite it, and test this Cypher statement to test this use case with the person, Tom Hanks.
MATCH (p:Actor)-[:ACTED_IN]-(m:Movie)
WHERE p.name = 'Tom Hanks' AND
'Drama' IN m.genres
RETURN m.title AS Movie
It should return the movies Apollo 13 and Sleepless in Seattle.
Execute The Query
Use the Sandbox window to UNWIND
the genres
property of each Movie, then create a new node and relationship.
Once you have done so, click Check Database to verify that the graph has been correctly refactored.
Validate Results
Once you have run the query, click the Check Database button and we will check the database for you.
Hint
Your query should create 6 nodes and 10 relationships.
Remember to create the new Genre node, a relationship between the Genre and Movie, and then remove the .genres
property on the node by setting it to null
.
Solution
Run the following query to refactor your graph.
//refactor code
MATCH (m:Movie)
UNWIND m.genres AS genre
MERGE (g:Genre {name: genre})
MERGE (m)-[:IN_GENRE]->(g)
SET m.genres = null;
//revised query
MATCH (p:Actor)-[:ACTED_IN]-(m:Movie)--(g:Genre)
WHERE p.name = 'Tom Hanks' AND
g.name = 'Drama'
RETURN m.title AS Movie
Once you have done so, click Try Again to verify that the graph has been correctly refactored.
Summary
In this challenge, you demonstrated that you can refactor the graph to add nodes to replace duplication in properties.
Your instance model should now look like this:
In the next lesson, you will learn some more refactoring tips to eliminate duplicate data that is complex.