Patterns
You find data in Neo4j by matching patterns using Cypher.
This pattern represents action movies:
(m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
The pattern consists of:
-
All nodes with a label of
Movie, -
that have an outgoing
IN_GENRErelationship -
to a node with a label of
:Genre -
that has property of
namewith a value of "Action".
MATCH
You can use a pattern to find data using the MATCH clause:
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
RETURN m.title, g.nameMATCH
Patterns can contain multiple nodes and relationships. For example, finding all the actors in action movies:
MATCH (actor:Person)-[:ACTED_IN]->(m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
RETURN actor.name, m.title, g.nameMultiple patterns
You can use multiple MATCH clases to find data distributed across the graph.
Finding the directors of action movies:
MATCH (actor:Person)-[:ACTED_IN]->(m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
MATCH (m)<-[:DIRECTED]-(director:Person)
RETURN actor.name, director.name, m.title, g.nameOptional MATCH
You may need to find data that may or may not exist in the graph.
For example, you want to find all the movies for a certain genre and their ratings, but some movies don’t not have a rating:
This query uses OPTIONAL to find "Film-Noir" movies and the users who rated them.
Any movie that does not have a rating will return null for the user’s name and rating.
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre {name: "Film-Noir"})
OPTIONAL MATCH (m)<-[r:RATED]-(u:User)
RETURN m.title, u.name, r.ratingDistinct Rows
Cypher will return all the rows that match the pattern.
If you want to return only unique rows, you can use the DISTINCT keyword.
This query returns the names of all the actors who have acted with Tom Hanks.
MATCH (p:Person {name: "Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(p2:Person)
RETURN DISTINCT p.name, p2.nameThe DISTINCT keyword ensures that each actor is only returned once, even if they have acted in multiple movies with Tom Hanks.
Alias
You can use the AS keyword to give a name to the data you are returning.
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)
RETURN m.title AS movieTitle, g.name AS genreChallenges
Complete the queries to find the following data in the graph:
-
A movie you like.
cypherMATCH (m:??????) RETURN m.title AS movieTitle -
Use the
ACTED_INrelationship to find who acted in that movie.cypherMATCH (m:Movie {title: "???"})<-[:??????]-(p:Person) RETURN p.name AS actor -
Use the DIRECTED relationship to find who directed the movie.
cypherMATCH (m:Movie {title: "???"})<-[:??????]-(p:Person) RETURN p.name AS director
Click to reveal the answers
-
A movie you like:
cypherMATCH (m:Movie) RETURN m.title AS movieTitle -
Use the
ACTED_INrelationship to find who acted in that movie.cypherMATCH (m:Movie {title: "Jumanji"})<-[:ACTED_IN]-(p:Person) RETURN p.name AS actor -
Use the DIRECTED relationship to find who directed the movie.
cypherMATCH (m:Movie {title: "Jumanji"})<-[:DIRECTED]-(p:Person) RETURN p.name AS director
Next
Summary
In this lesson, you learned how to match data in the graph using patterns.