Cypher Query Language to query Neo4j

3 minute read

Querying Graph Databases

When you need to query a graph database, you need to define the graph pattern first.

A graph pattern is a graphical and logical way to identify the graph resources (nodes and edges) you want to query.

You can think of a graph pattern as a subgraph of the whole graph you will query.

Cypher uses an ASCII-Art syntax to represent the graph pattern. That’s a very good approach because you can graphically think about what you need to query to create the Cypher query. In Cypher, nodes are referred with () and edges with []. One of the simplest graph patterns is the following: (:label_1)-[:type_1]->(:label2) which represents a relationship between two nodes (the first labelled with label_1 and the second labelled with label_2) through an arch whose type is type_1.

Here we will think about the graph patterns to apply to the graph database and then you will perform the queries with Cypher.

After you define the graph pattern, you need to use the Cypher MATCH clause to query the database and reach all the subgraphs that match the graph pattern you defined.

Populate the graph DB with example data

Before you can perform the queries, you need to populate the graph DB with some data. Use the code (Cypher data creation clauses) given here and execute it from your Neo4j browser.

CREATE (andy:Driver {name: "Andy Smith"}), (john:Driver {name: "John Town"}), (bryan:Driver {name: "Bryan Tell"}), (ellie:User {name: "Ellie Cast"}), (ta:Team {name: "Team A", country: "Nevada"}), (tb:Team {name: "Team B", country: "California"})
CREATE (andy)-[m:MARRIED_WITH]->(ellie)
CREATE (andy)-[d1:DRIVES {year: 2017}]->(ta)
CREATE (john)-[d2:DRIVES {year: 2019}]->(ta)
CREATE (john)-[fo1:FRIENDS_OF]->(bryan)
CREATE (bryan)-[fo2:FRIENDS_OF]->(john)
CREATE (bryan)-[d3:DRIVES {year: 2020}]->(tb)
CREATE (ta)-[e1:ENEMIES_OF]->(tb)
CREATE (tb)-[e2:ENEMIES_OF]->(ta)
RETURN andy, john, bryan, ellie, ta, tb, d1, d2, d3, fo1, fo2, e1, e2;

This will create the following graph:

Queries

1. Simple MATCH query

Enter the following Cypher commands, and click on the Play button to execute the first query:

MATCH (d:Driver)
RETURN d;

The d is an alias that you associate to a node or edge in the pattern. You can use it to return the specific resource or to perform a filter using the WHERE clause.

This is the simplest query you can perform. It queries all the nodes with a particular label. The result is as follow:

2. MATCH with WHERE clause

Example of using WHERE clause

MATCH (d:Driver)
WHERE d.name = "Andy Smith"
RETURN d;

This graph pattern will return all nodes labelled as Driver, and it also filters on properties (in this case on the property name). The result will be a single node with Andy Smith as the name.

You can avoid inserting the WHERE clause and filter on properties. This query is equivalent to the previous one:

MATCH (d:Driver {name: "Andy Smith"})
RETURN d;

3. MATCH both nodes and relationships

Next lets look at another example:

MATCH (d:Driver {name: "John Town"})-[r:DRIVES]->(t:Team {name: "Team A"})
RETURN d, r, t;

This graph pattern will return all nodes labelled as Driver and Team that has a relationship of type DRIVES. It also performs a filter on nodes’ properties. The result is as follow:

4. Multiple nodes and relationships

MATCH (u:User)<-[m:MARRIED_WITH]-(d:Driver)-[dr:DRIVES]->(t:Team {name: "Team A"})
RETURN u, m, d, dr, t;

This graph pattern will return all nodes labelled as Driver that have a relationship of type MARRIED_WITH and that have a DRIVES relationship with the node that represents the Team A. The result is as follow:

5. Multiple nodes and relationships II

MATCH (d1:Driver)-[r1:DRIVES]->(t:Team {name: "Team A"})<-[r2:DRIVES]-(d2:Driver)
RETURN d1, r1, t, r2, d2;

This graph pattern will return the couples of Driver nodes that have a relationship of type DRIVES with the node that represents the Team A. The result is as follow:

6. Multiple nodes and relationships III

MATCH (ta:Team {name: "Team A"})<-[dr1:DRIVES]-(d1:Driver)-[fr:FRIENDS_OF]->(d2:Driver)-[dr2:DRIVES]->(tb:Team {name: "Team B"})
RETURN ta, dr1, d1, fr, d2, dr2, tb;

This graph pattern will return all Driver nodes that have a DRIVES relationship with the Team A node and that have a FRIENDS_OF relationship with a Driver node that has a DRIVES relationship with the Team B node. The result is as follow:

Cypher cheat sheet

Tags:

Updated: