In the previous Challenge, you added a number of specialized relationships to the graph for the ACTED_IN and DIRECTED relationships. This Challenge has 3 steps.
In our current graph, there are RATED relationships between User nodes and Movie nodes.
Suppose we wanted to improve the performance of this query:
Use case #9: What users gave a movie a rating of 5?
Why create specialized relationships?
Let’s take a practical example. Run this Cypher code to test this use case with the movie, Apollo 13.
MATCH (u:User)-[r:RATED]->(m:Movie)
WHERE m.title = 'Apollo 13' AND
r.rating = 5
RETURN u.name as Reviewer
It should return one User, Sandy Jones.
What if there were thousands of Users in the graph. This query would need to traverse all RATED relationships and evaluate the rating property. For a large graph, more evaluations mean longer query processing time.
In this challenge, you will specialize the RATED relationships to reflect the rating. Unlike the refactoring where we removed the genres and languages properties from the nodes, we will not remove the rating property from the RATED relationship. This is because we may need it for a query that has a reference to the relationship and needs to return the rating value.
This is the instance model you will refactor toward:
Creating specialized RATED_{rating} Relationships
To pass this challenge, you must use the knowledge you gained in the previous lesson to merge a relationship between the graph using apoc.merge.relationship
.
The pattern you must search for is:
MATCH (u:User)-[r:RATED]→(m:Movie)
The relationship type passed as the second parameter should be:
'RATED_'+ r.rating
Verifying the Refactoring
Validate Results
Once you have run the query, click the Check Database button and we will check the database for you.
Hint
If you are stuck, you can run CALL apoc.help("merge.relationship")
in the Sandbox window to get more information on the procedure or read the documentation here.
Solution
To complete this challenge, click Run in Sandbox to execute the following Cypher statement:
MATCH (n:User)-[r:RATED]->(m:Movie)
CALL apoc.merge.relationship(n,
'RATED_' + r.rating,
{},
{},
m,
{}
) YIELD rel
RETURN COUNT(*) AS `Number of relationships added`;
Once you have run the query, click Try again… to verify that it has been successfully run.
If you are still stuck, you can try refreshing your browser to reset the graph.
Summary
In this challenge, you demonstrated that you can refactor the graph to specialize the RATED relationships.
In the next module, you will learn about creating intermediate nodes.