Finding Relationships

Finding relationships

Our Goal

As a movie fanatic
I would like to find movies for a particular actor
So that I can watch a movie this evening

In the previous lesson, we used the MATCH clause to find the node in our database that represented Tom Hanks.

cypher
MATCH (p:Person {name: 'Tom Hanks'})
RETURN p

We can extend the pattern in the MATCH clause to traverse through all relationships with a type of ACTED_IN to any node. Our domain model shows that the ACTED_IN relationship goes in an outgoing direction from the Person node so we can add the direction in our pattern. We often refer to this as a traversal.

cypher
Incomplete code
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->()

Our data model dictates that the node at the other end of that relationship will be Movie node, so we don’t necessarily need to specify the :Movie label in the node - instead we will use the variable m.

cypher
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m)
RETURN m.title

This code returns the titles of all movies that Tom Hanks acted in.

If our graph had different labels, for example Television and Movie nodes this query would have returned all Television and Movie nodes that Tom Hanks acted in. That is, if we had multiple types of nodes at the end of the ACTED_IN relationships in our graph, we could make sure that we only return movies.

cypher
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m:Movie)
RETURN m.title

Because our graph only has Movie nodes that have incoming ACTED_IN relationships, this query returns the exact same results as the previous query.

Check your understanding

1. Specifying the relationship direction

Complete the following Cypher query that starts with the Movie node.

Use the dropdown below to select the correct relationship direction.

cypher
MATCH (m:Movie)/*select:<-[:ACTED_IN]-*/(p:Person)
RETURN m.title, p.name
  • -[:ACTED_IN]->

  • <-[:ACTED_IN]-

Hint

How is the ACTED_IN relationship defined between Person nodes and Movie nodes?

The and symbols specify the direction of the relationship.

Solution

In the graph, the :ACTED_IN relationship always goes from a :Person node to a :Movie node.

As the :Movie node is defined as the first node in the pattern, the answer is ←[:ACTED_IN]-.

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
RETURN m.title, p.name

2. Query using a pattern

There is a DIRECTED relationship between the Person nodes and the Movie nodes - it represents the people who directed the movies.

Which MATCH clauses will return the names of people who directed movies? (Select all that apply)

  • MATCH (m:Movie)<-[:DIRECTED]-(p:Person) RETURN p.name

  • MATCH (m:Movie)--(p:Person) RETURN p.name

  • MATCH (m:Movie)<-[:DIRECTED]-(p) RETURN p.name

  • MATCH (m:Movie)-[:DIRECTED]→(p:Person) RETURN p.name

Hint

The Person nodes represent people and the relationship must be DIRECTED.

You don’t need to define the :Person label for the node at the end of the pattern.

Solution

The two options below are valid options for finding the people who directed movies:

MATCH (m:Movie)←[:DIRECTED]-(p:Person) RETURN p.name
MATCH (m:Movie)←[:DIRECTED]-(p) RETURN p.name

The following query, will return the directors but because the relationship type or direction type is not defined, you will receive all people connected to that movie, including anyone who acted in the movie through the :ACTED_IN relationship.

MATCH (m:Movie {title: 'The Matrix'})--(p:Person) RETURN p.name

The following query won’t return any results because the direction of the relationship is defined as instead of .

MATCH (m:Movie)-[:DIRECTED]→(p:Person) RETURN p.name

Summary

In this lesson, you learned how to traverse patterns in the graph with your queries using relationships. In the next challenge, you will demonstrate your skills for traversing the graph.