Pattern Matching

In the previous lesson, you learned how Cypher can be used to read data.

In this lesson, you will explore how you match data with patterns.

Patterns

Patterns

Cypher is a declarative query language that allows you to identify patterns in your data using an ASCII-art style syntax consisting of brackets, dashes and arrows.

This pattern finds all nodes with a label of Person, that have an outgoing ACTED_IN relationship to a node with a label of :Movie:

(p:Person)-[r:ACTED_IN]→(m:Movie)

If you break this pattern down, it contains nodes, relationships and variables.

Nodes

Nodes in the pattern are expressed with parentheses - ( )

parenthesis ( ) highlighted

Inside the parentheses you can define information about the node, for example the label(s) or properties the node should contain.

:Person and :Movie highlighted

Labels are prefixed a colon - (:Label)

The pattern contains two nodes (:Person) and (:Movie).

Relationships

Relationships are drawn with two dashes (--) and an arrow to specify the direction (< or >) - - →.

dashes - → highlighted

Relationship information is contained within square brackets -[ ].

:ACTED_IN highlighted

The relationship type is prefixed with a colon - [:TYPE]

The pattern contains one relationship -[:ACTED_IN]→ between (:Person) and (:Movie) nodes.

To recap, this pattern:

(p:Person)-[r:ACTED_IN]→(m:Movie)

Finds (:Person) nodes, that have an -[:ACTED_IN]→ relationship, to (:Movie) nodes.

Variables

The nodes and relationships in the pattern are assigned to variables.

p

These variables are positioned before the information about the node or relationship.

  • p - the :Person node

  • r - the :ACTED_IN relationship

  • m - the :Movie node

MATCH-ing

The MATCH clause is used to find patterns in the data.

Review this Cypher statement, you should be able to identify:

  1. The pattern being used.

  2. A WHERE clause which filters the results.

  3. The variables used in the RETURN clause.

cypher
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN p,r,m

Patterns can be as simple as a single node, or contain multiple relationships.

This example finds all people who have acted in movies with 'Tom Hanks', and uses the RETURN clause to define the properties.

cypher
Actors who acted with Tom Hanks
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[r:ACTED_IN]-(p2:Person)
WHERE p.name = 'Tom Hanks'
RETURN p2.name AS actor, m.title AS movie, r.role AS role

The pattern uses the ACTED_IN relationship to find the movies Tom Hanks is in, and then a second time to find the actors in the movies with Tom Hanks,

The keyword AS is used to define an alias, for example, the property p2.name will be returned as actor.

Try it yourself

Emil Eifrem, Neo4j’s CEO, has added himself to the graph as an actor in his favorite movie. Can you find out which movie is his favorite?

Which movie is Emil’s favorite?

This Cypher statement returns the roles and movies 'Tom Hanks' has acted in.

You will need to modify it to use the name 'Emil Eifrem'.

cypher
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN p.name AS person, m.title AS title, r.role AS role

What movie is returned?

  • ❏ Back to the Future

  • ❏ Blade Runner

  • ❏ Gravity

  • ✓ The Matrix

Hint

You will need to modify the WHERE clause from filtering for 'Tom Hanks' to filter for Emil Eifrem

Strings are case-sensitive so you will need to use the correct capitalization of *E*mil *E*ifrem.

The answer is the title property of the (:Movie) node.

Solution

You can use the following query to find the title of the Movie that Emil is listed as acted in.

cypher
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Emil Eifrem'
RETURN p.name AS person, m.title AS title, r.role AS role

Click the Run in Sandbox button to reveal the answer in the Sandbox window.

Summary

In this lesson, you learned about Cypher patterns and how to read data.

In the next lesson, you will learn how to create data.