Specializing ACTED_IN and DIRECTED Relationships

In this Challenge, you will modify the instance model to match the following diagram. This diagram uses specialized ACTED_IN and DIRECTED relationships.

Specialized ACTED_IN and DIRECTED relationships

This Challenge has 2 steps:

Refactor all ACTED_IN relationships

Execute the following code to create a new set of relationships based on the year of the released property for each Node.

For example, Apollo 13 was released in 1995, so an additional ACTED_IN_1995 will be created between Apollo 13 and any actor that acted in the movie.

cypher
Creating Specialized Relationships
MATCH (n:Actor)-[:ACTED_IN]->(m:Movie)
CALL apoc.merge.relationship(n,
  'ACTED_IN_' + left(m.released,4),
  {},
  {},
  m ,
  {}
) YIELD rel
RETURN count(*) AS `Number of relationships merged`;

It should create 5 relationships.

With this refactoring, we can now confirm that our rewritten query works for the use case:

Use case #12: What movies did an actor act in for a particular year?

To verify the query has run successfully, we can attempt to use the newly created ACTED_IN_1995 relationship to see which Movies Tom Hanks acted in that were released in 1995.

cypher
Tom Hanks Movies released in 1995
MATCH (p:Actor)-[:ACTED_IN_1995]->(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN m.title AS Movie

It should return one movie from our dataset, Apollo 13.

Refactor all DIRECTED relationships

We can use the same method to create DIRECTED_{year} relationships between the Director and the Movie.

Modify the code you have just run to match the following pattern.

MATCH (n:Director)-[:DIRECTED]→(m:Movie)

Then modify the procedure call change the prefix of the relationship to DIRECTED_.

It should create 2 relationships.

Testing the Model

With this refactoring and the previous refactoring, we can now confirm that our rewritten query works for the use case:

Use case #12: What movies did an actor act in for a particular year?

cypher
MATCH (p:Person)-[:ACTED_IN_1995|DIRECTED_1995]->()
RETURN p.name as `Actor or Director`

It should return Tom Hanks and Martin Scorsese.

Validate Results

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

Hint

When calling the apoc.merge.relationship() procedure the second query, the second parameter will be 'DIRECTED_'+ left(m.released, 4).

If the numbers do not match the ones stated in the lesson above, please refresh your browser to reset the lesson.

Solution

To pass this challenge you must run the two queries below in order.

First, use the m.released property to create an ACTED_IN_{year} relationship between the Actor and the Movie:

cypher
1. Create ACTED_IN_{year} relationships
MATCH (n:Actor)-[:ACTED_IN]->(m:Movie)
CALL apoc.merge.relationship(n,
  'ACTED_IN_' + left(m.released,4),
  {},
  {},
  m ,
  {}
) YIELD rel
RETURN count(*) AS `Number of relationships merged`;

This query should create 5 relationships.

Then, use the same property to create a DIRECTED_{year} relationship between the Director and the Movie:

cypher
2. Create DIRECTED_{year} relationships
MATCH (n:Director)-[r:DIRECTED]->(m:Movie)
CALL apoc.merge.relationship(n,
  'DIRECTED_' + left(m.released,4),
  {},
  {},
  m,
  {}
) YIELD rel
RETURN COUNT(*) AS `Number of relationships added`

This query should create 2 relationships.

Then click Try Again.

If the numbers do not match, please refresh your browser to reset the graph.

Summary

In this Challenge, you demonstrated that you can refactor the graph to specialize the ACTED_IN and DIRECTED relationships.

In the next Challenge, you will refactor the RATED relationship.