Goals This guide explains the basic concepts of Cypher, Neo4j’s query language. You should be able to read and understand Cypher queries after finishing this guide. Prerequisites You should have familiarized yourself with Graph Databases and the Property Graph Model.… Read more →
This guide explains the basic concepts of Cypher, Neo4j’s query language. You should be able to read and understand Cypher queries after finishing this guide.
You should have familiarized yourself with Graph Databases and the Property Graph Model.
Cypher is a declarative, SQL-inspired language for describing patterns in graphs visually using an ascii-art syntax.
It allows us to state what we want to select, insert, update or delete from our graph data without requiring us to describe exactly how to do it.
Cypher uses ASCII-Art to represent patterns.
We surround nodes with parentheses which look like circles, e.g.
If we later want to refer to the node, we’ll give it a variable like
(p) for person or
(t) for thing.
In real-world queries, we’ll probably use longer, more expressive variable names like
If the node is not relevant to your question, you can also use empty parentheses
Usually, the relevant labels of the node are provided to distinguish between entities and optimize execution, like
We might use a pattern like
(person:Person)-->(thing:Thing) so we can refer to them later, for example, to access properties like
The more general structure is:
Please note that node-labels, relationship-types and property-names are case-sensitive in Cypher. All the other clauses, keywords and functions are not, but should be cased consistently according to the style used here.
To fully utilize the power of our graph database we want to express more complex patterns between our nodes.
Relationships are basically an arrow
--> between two nodes.
Additional information can be placed in square brackets inside of the arrow.
This can be
a variable name
-[rel:KNOWS]->before the colon
structural information for paths of variable length
To access information about a relationship, we can assign it a variable, for later reference.
It is placed in front of the colon
-[rel:KNOWS]-> or stands alone
If you forget the colon in front of a relationship-type, like this
Nodes and relationship expressions are the building blocks for more complex patterns. Patterns can be written continuously or separated with commas. You can refer to variables declared earlier or introduce new ones.
path = shortestPath( (user)-[:KNOWS*..5]-(other) )
Patterns can be used to
Let’s try out what we’ve learned. See an interactive live example of the exercise below.
First Steps with Cypher
Create a Record for Yourself
CREATE creates nodes with labels and properties.
You like Neo4j, right?
Let’s find ourselves and add a new relationship to a new node.
CREATE can create single nodes, or more complex structures.
Create Your Friends
FOREACH allows you to execute update operations for each element of a list.
Find Your Friends
Note that we get ourselves repeated for each path found in the graph.
Create Second Degree Friends and Expertise
CREATE can also add more complex patterns.