Neo4j Cypher query language

What is Cypher?

Video Thumbnail

Overview

Visual, intuitive, and powerful graph data query language

Cypher’s easy-to-learn pattern constructs make it accessible for developers, data scientists, and those with limited query language experience. Users can simply express what data to retrieve while the underlying engine completes the task – eliminating the need for technical implementation knowledge.

Neo4j and Cypher under the hood

Cypher is an expressive language with advanced graph patterns and collection support. Under the hood, the cypher processing pipeline first parses the query if not in cache, then goes through semantic verification and rewriting of the AST, followed by finding the cheapest execution plan (logical and physical) for all the operations using available planners, all the way to query execution.

Run your own Cypher query right now

Following is an example that demonstrates how easy it is to use Cypher using two simple datasets, TMDB (Movies) and Northwind (Retail). (You can also install these datasets directly into your Neo4j database by running :play movies and :play northwind in the Neo4j Browser and execute the load statements.) This example also shows how much easier it is to use Cypher compared to SQL.

Follow a chain of relationships to an arbitrary depth and return the full path.

Cypher

MATCH path =
   (p:Product {productName:'Pavlova'})-[:PART_OF*]->(root:Category)
RETURN path

Find the shortest connection between two entities, here the “Bacon-Path.”

Cypher

MATCH (bacon:Person {name:'Kevin Bacon'})
MATCH path = shortestPath(
      (:Person {name:'Meg Ryan'})-[:ACTED_IN*..10]-(bacon))
RETURN path, length(path);

In Cypher, reads and writes can be combined in a single statement, which allows the full power of the language to select data to update and to return updated data directly to the user.

Cypher

// read
MATCH (m:Movie {title:'Matrix Resurrections'})
// possibly write
MERGE (u:User {name:'Emil Eifrem'})
// possibly write
MERGE (u)-[r:RATED]->(m)
// write
SET r.stars = 5
WITH *
MATCH (m)<-[:ACTED_IN]-(a:Person)
// read & return
RETURN u, m, r, collect(a)

With Cypher you can use query pipelining with WITH and implicit grouping to simplify aggregations and multi-part analytics queries.

Cypher

MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
// in-between aggregation, only passing on declared fields
WITH a, count(m) as movies
// with filter on the aggregated value
WHERE movies > 2
// next query part
MATCH (a)-[:DIRECTED]->(m)
// aggregation at the end by a.name
RETURN a.name, collect(m.title) as movies, count(*) as count
ORDER BY count DESC LIMIT 5;

Load CSV, JSON and other data types from other data sources.

Cypher

WITH "https://data.neo4j.com/importing/ratings.csv" AS url
LOAD CSV WITH HEADERS FROM url AS row
MATCH (m:Movie {id:row.movieId})
MERGE (u:User {userId:row.userId})
ON CREATE SET u.name = row.name
MERGE (u)-[r:RATED]->(m)
SET r.rating = toFloat(row.rating)
SET r.timestamp = toInteger(row.timestamp);

Cypher

MATCH path =
   (p:Product {productName:'Pavlova'})-[:PART_OF*]->(root:Category)
RETURN path

Find the shortest connection between two entities, here the “Bacon-Path.”

Cypher

MATCH (bacon:Person {name:'Kevin Bacon'})
MATCH path = shortestPath(
      (:Person {name:'Meg Ryan'})-[:ACTED_IN*..10]-(bacon))
RETURN path, length(path);

Learn more about these features or run :play cypher-vs-sql in the Neo4j Browser.