Matching Data

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:

  1. All nodes with a label of Movie,

  2. that have an outgoing IN_GENRE relationship

  3. to a node with a label of :Genre

  4. that has property of name with a value of "Action".

MATCH

You can use a pattern to find data using the MATCH clause:

cypher
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
RETURN m.title, g.name

MATCH

Patterns can contain multiple nodes and relationships. For example, finding all the actors in action movies:

cypher
MATCH (actor:Person)-[:ACTED_IN]->(m:Movie)-[:IN_GENRE]->(g:Genre {name: "Action"})
RETURN actor.name, m.title, g.name

Multiple patterns

You can use multiple MATCH clases to find data distributed across the graph.

Finding the directors of action movies:

cypher
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.name

Optional 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.

cypher
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre {name: "Film-Noir"})
OPTIONAL MATCH (m)<-[r:RATED]-(u:User)
RETURN m.title, u.name, r.rating

Distinct 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.

cypher
MATCH (p:Person {name: "Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(p2:Person)
RETURN DISTINCT p.name, p2.name

The 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.

cypher
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)
RETURN m.title AS movieTitle, g.name AS genre

Challenges

Complete the queries to find the following data in the graph:

  1. A movie you like.

    cypher
    MATCH (m:??????) RETURN m.title AS movieTitle
  2. Use the ACTED_IN relationship to find who acted in that movie.

    cypher
    MATCH (m:Movie {title: "???"})<-[:??????]-(p:Person)
    RETURN p.name AS actor
  3. Use the DIRECTED relationship to find who directed the movie.

    cypher
    MATCH (m:Movie {title: "???"})<-[:??????]-(p:Person)
    RETURN p.name AS director
Click to reveal the answers
  1. A movie you like:

    cypher
    MATCH (m:Movie) RETURN m.title AS movieTitle
  2. Use the ACTED_IN relationship to find who acted in that movie.

    cypher
    MATCH (m:Movie {title: "Jumanji"})<-[:ACTED_IN]-(p:Person)
    RETURN p.name AS actor
  3. Use the DIRECTED relationship to find who directed the movie.

    cypher
    MATCH (m:Movie {title: "Jumanji"})<-[:DIRECTED]-(p:Person)
    RETURN p.name AS director

Summary

In this lesson, you learned how to match data in the graph using patterns.