Pattern Matching

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

Labels

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 types

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.

Recap

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

MATCH-ing

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.

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.