Adding the Actor Label

Here is the refactored instance model we will create in the graph where we add an Actor label to some of the Person nodes:

Added Actor label

Profile the query

Execute this query:

cypher
PROFILE MATCH (p:Person)-[:ACTED_IN]-()
WHERE p.born < '1950'
RETURN p.name

In the first step of this query, we see that 5 Person rows are returned.

Refactor the graph

With Cypher, you can easily transform the graph to add Actor labels.

Execute this Cypher code to add the Actor label to the appropriate nodes:

cypher
Add Actor Labels
MATCH (p:Person)
WHERE exists ((p)-[:ACTED_IN]-())
SET p:Actor

There are 5 Person nodes in the graph, but only 4 have an :ACTED_IN relationship. Therefore, the query above should apply the Actor label to four of the five Person nodes.

Profile the query

Now that we have refactored the graph, we must change our query and profile again.

Execute this query:

cypher
PROFILE MATCH (p:Actor)-[:ACTED_IN]-()
WHERE p.born < '1950'
RETURN p.name

In the first step of this query, we see that 4 Actor rows are returned.

Validate Results

Once you have run the code to add the Actor labels, click the Check Database button and we will check the database for you.

Hint

You will need to run the Add Actor Labels query above to complete this challenge.

After running the query, four of the five Person nodes should also have an additional Actor label.

You can run the following query to check the number of Actor nodes in the database.

cypher
Check Actor Numbers
MATCH (a:Actor)
RETURN count(a) AS count, count(a) >= 4 AS shouldValidate

Solution

Click Run in Sandbox to execute the query below.

cypher
Solution
MATCH (p:Person)
WHERE exists ((p)-[:ACTED_IN]-())
SET p:Actor

If this does not work, reload this page to reset your Sandbox and then run the query again.

Summary

In this challenge, you demonstrated that you can refactor the graph to add the Actor label to some of its nodes and profile queries before and after the refactoring.

In the next lesson, you will learn that you must retest your use cases after you refactor.