Adding Language nodes

This is what the instance model will be refactored to:

Instance model with language node

Creating Language Nodes

Execute this code to refactor the graph to turn the languages property values into Language nodes:

cypher
Creating Language Nodes
MATCH (m:Movie)
UNWIND m.languages AS language

MERGE (l:Language {name:language})
MERGE (m)-[:IN_LANGUAGE]->(l)
SET m.languages = null

Modifying the Cypher statement

This is the Cypher code for the use case before the refactoring:

cypher
Previous Query
MATCH (m:Movie)
WHERE 'Italian' IN m.languages
RETURN m.title

This query can now be modified to use the newly-created Language node:

cypher
Using the Language Node
MATCH (m:Movie)-[:IN_LANGUAGE]-(l:Language)
WHERE  l.name = 'Italian'
RETURN m.title

This is the only use case that deals with languages so we need not retest all of our queries after the refactor.

Validate Results

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

Hint

You will need to run the Cypher statement in Creating Language Nodes to perform this refactoring.

Then click Try Again.

Solution

The following Cypher will create the Language nodes and IN_LANGUAGE relationships to the Movie nodes.

cypher
MATCH (m:Movie)
UNWIND m.languages AS language

MERGE (l:Language {name:language})
MERGE (m)-[:IN_LANGUAGE]->(l)
SET m.languages = null

Summary

In this challenge, you demonstrated that you can refactor the graph to add nodes to replace duplication in properties.

In the next challenge, you will perform some additional factoring to eliminate duplication.