Adding Genre nodes

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.

cypher
MATCH (m:Movie)
RETURN m.title, m.genres

Your 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:

  1. 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.

  2. Delete the genres property from the Movie nodes.

cypher
Cypher to create Language nodes
MATCH (m:Movie)
UNWIND m.languages AS language
MERGE (l:Language {name:language})
MERGE (m)-[:IN_LANGUAGE]->(l)
SET m.languages = null

You can check the outcome of the refactoring by running this query:

cypher
MATCH (m:Movie-[:IN_GENRE]->(g:Genre)
RETURN m.title, g.name

Your 2nd challenge, is to rewrite this query, that uses the genres property, to use the newly created Genre nodes and IN_GENRE relationship.

cypher
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.

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.

cypher
MATCH (m:Movie)
UNWIND m.genres AS genre

MERGE (g:Genre {name: genre})
MERGE (m)-[:IN_GENRE]->(g)
SET m.genres = null

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:

Instance Model thus far

In the next lesson, you will learn some more refactoring tips to eliminate duplicate data that is complex.