In the previous Challenge, you eliminated duplication by taking the data in the languages property and creating Language nodes that are related to movies.
The Movie nodes have a genres property that contains a list of genres for each movie.
MATCH (m:Movie)
RETURN m.title, m.genresYour challenge is to refactor the graph to create Genre nodes and connect them to the Movie nodes using an IN_GENRE relationship.
Modify and execute the query from the previous challenge to:
-
Use the data in the genres property for the Movie nodes to create Genre nodes using the IN_GENRE relationship to connect Movie nodes to Genre nodes.
-
Delete the genres property from the Movie nodes.
MATCH (m:Movie)
UNWIND m.languages AS language
MERGE (l:Language {name:language})
MERGE (m)-[:IN_LANGUAGE]->(l)
SET m.languages = nullYou can check the outcome of the refactoring by running this query:
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)
RETURN m.title, g.nameYour 2nd challenge, is to rewrite this query, that uses the genres property, to use the newly created Genre nodes and IN_GENRE relationship.
MATCH (p:Actor)-[:ACTED_IN]-(m:Movie)
WHERE p.name = 'Tom Hanks'
AND 'Drama' IN m.genres
RETURN m.title AS MovieIt should return the movies Apollo 13 and Sleepless in Seattle.
Validate Results
Once you have added the Genre nodes, click the Check Database button and we will check the database for you.
Hint
Your query should use the genres property to create Genre nodes and connect them to the Movie nodes using the IN_GENRE relationship.
Solution
Run the following query to refactor your graph.
MATCH (m:Movie)
UNWIND m.genres AS genre
MERGE (g:Genre {name: genre})
MERGE (m)-[:IN_GENRE]->(g)
SET m.genres = nullSummary
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.