Adding labels

Moving toward the data model

Our data model has additional labels, Actor and Director.

Movie data model

Having these specific labels is a best practice so that key queries will perform better, especially when the graph is large.

We must transform the Person nodes to have these specific labels. If a person acted in a movie, then they will be labeled as an actor. If a person directed a movie, they will be labeled as a director.

Adding the labels

Here is the code that we can use the add the Actor label to all nodes that have the ACTED_IN relationship:

You will execute this code in the next Challenge.

Cypher
Unresolved directive in lesson.adoc - include::{repository-raw}/main/modules/3-refactoring-imported-data/lessons/5-add-labels/set-actor-label.cypher[]

Check your understanding

Adding a label to a node

We want to add the Director label to all nodes that have the DIRECTED relationship to a Movie node. How would you add the label in the code below:

Once you have selected your option, click the Check Results query button to continue.

cypher
MATCH (p:Person)-[:DIRECTED]->()
WITH DISTINCT p
/*select:SET p:Director*/
  • CALL p.setLabel('Director')'

  • SET p:Director

  • p.labels=["Person","Director"]

Hint

You use the same keyword you use for assigning values to properties here.

Solution

The correct answer is: SET p:Director

Summary

In this lesson, you learned how to add a label to a set of nodes. In the next challenge, you will add the Actor and Director labels to the graph.