Cypher
Part 1: Basics and
Queries
Prof.
gianmaria.silvellounipd.it
http://www.dei.unipd.it/~silvell
o/
Master course in Computer
Outlin
e
Running example
Cypher Basics
Overview Values and
Data Types Writing
Queries
Filtering Query Results
Advanced Query
Processing 2
Running
Example
Running example
Created with:
https://arrows.app/ 4
Export in
Cypher
5
Import in
Neo4j
6
Cypher Basics
Overview
Basics
Cypher is based on graph
patterns
MATCH
(p1:Actor)-[:ACTED]->(f:Film)<-[:DIRECTED]-(p2:Person)-[:FRIEND_OF]-(p1)
RETURN p1,p2
Patterns use AsciiArt
Nodes are represented with
parenthesis Arcs with arrows
8
Ascii Art for
Nodes
Nodes are surrounded by
parenthesis
() or (p)
Labels, or tags, start with : and group nodes by roles or types
(p:Person:Actor) or (p:Director)
Nodes can have properties
(p:Person {name:’Quentin
Tarantino’})
9
Ascii Art for Relationships
Relationships are wrapped with hyphens or square brackets
--> or -[a:ACTED]->
a is a variable
:ACTED is a label
Direction of relationship is specified with < >
(p)-[:ACTED]->(f) or (f)<-[:ACTED]-(p)
p and f are variables (references/aliases that can be accessed later in the
query)
Relationships can have properties
-[:ACTED
{role:’father’}]->
10
Anonymous
nodes
If we do not need to refer to a specific node later in the query, we do not
need to assign a variable to it.
We can use anonymous nodes: ( )
This means that you will not be able
to return this node later in the query.
MATCH
(p1:Actor)-[:ACTED]->()<-[:DIRECTED]-(p2:Person)-[:FRIEND_OF]-(p1)
RETURN p1,p2
Part Meaning
MATCH Starts a pattern match in the graph.
Matches a node labeled Actor and binds it to variable
(p1:Actor)
p1.
Finds a relationship ACTED from p1 to some movie
-[:ACTED]->()
node (unnamed).
Finds a DIRECTED relationship from a Person node p2
<-[:DIRECTED]-(p2:Person)
to the same movie.
-[:FRIEND_OF]-(p1) Ensures that p2 is also friends with p1 (the Actor).
RETURN p1, p2 Returns both the Actor (p1) and the Person (p2).
11
Creating
Data
ACTED
CREATE (:Actor {name:’Peter’})-[:ACTED]->(:Film
{title:’Spacecraft’})
LABEL PROPERTY LABEL PROPERTY
NOD NOD
E E
12
The use of
aliases
ACTED
MATCH
(:Actor {name:’Peter’})-[:ACTED]->(f:Film)
RETURN
f
f = :Film
{title:’Spacecraft’}
13
Searching Alternatives by using
properties
ACTED
MATCH
e
l ik
(:Actor {name:’Peter’})-[:ACTED]->(f:Film)
N
RETURN
O
JS
f
MATCH
(a:Actor)-[:ACTED]->(f:Film)
e
WHERE
li k
L
a.name=’Peter’
SQ
RETURN
f
14
Setting new properties
The SET keyword is used to add properties to a node or a
relationship
MATCH
(a:Actor)-[:ACTED]->(f:Film)
WHERE
a.name=’Peter’
SET
f.duration=92
f.genre=‘sci-fi’
RETURN
f
15
Node uniqueness
name:’Peter’ name:’Peter’
If the name property is our (candidate) primary key,
we need to add the constraint to the graph, to
avoid that more than one node share the same
name name:’Peter’
name:’Peter’
name:’Peter’
name:’Peter’
16
Node uniqueness
CREATE CONSTRAINT ON
(a:Person) ASSERTa.name IS
UNIQUE
name:’Peter’ name:’Peter’
name:’Peter’
name:’Peter’
name:’Peter’
name:’Peter’
17
Add new relationships to an existing
node
DIRECTED
MERGE (a:Actor {name:’Peter’})
CREATE (a)-[:DIRECTED]->(:Film {title:’Love
boat’})
If Peter exists, then the relationship is created
If Peter does not exist, then the node ‘Peter’ is created and then the property is
added
18
Add new relationships to an existing
node
DIRECTED
MERGE (a:Actor {name:’Peter’})
ON CREATE SET
a.born=1989-10-01
MERGE (a)-[:DIRECTED]->(:Film {title:’Love boat’})
If Peter exists, then the property “born” is not set
If Peter does not exist, then the node ‘Peter’ is created and the property “born”
is set The last MERGE says that if that exact pattern does not exist then it is
19
Values and Data
Types
Data
types
Properties are name-value pairs that provide additional details to
nodes and relationships
Properties can have values with a variety of data types
Property types
Integer, Float, String, Boolean, Point, Date, Time, LocalTime, DateTime,
LocalDateTime, and Duration.
Structural types
Node, Relationship, and Path.
Composite types
List and Map.
21
Property Types
Can be returned from Cypher queries
Can be used as
parameters Can be stored
as properties
Can be constructed with Cypher
literals
22
Structural
Types
Can be returned from Cypher queries
Cannot be used as
parameters Cannot be
stored as properties
MERGE (a:Actor
Cannot be constructed {name:’Peter’})
with Cypher
literals CREATE (a)-[:DIRECTED]->(:Film {title:’Love
boat’})
23
Composite
Types
Can be returned from Cypher
queries
Can be used as parameters
Cannot be stored as
Can be constructed with Cypher List and
literals Map
https://neo4j.com/docs/cypher-manual/current/syntax/ /
values
properties
24
Data
types
MATCH (f:Film{title:'Pulp Fiction'})
SET f.duration =
RETURN
94 f
Cypher properties always have a type, but Neo4j doesn't constrain their
type
Nevertheless, you can convert a value to a specific data type and set it
MATCH (f:Film{title:'Pulp
SET f.duration =
Fiction'})
RETURN
toInteger(94)
f
25
Scalar functions
Doc: https://neo4j.com/docs/cypher-manual/current/functions/scalar/
The main scalar function we need are:
id() ➝ returns the identifier (an integer) of a node or relationship
properties(x) ➝ returns a map with the properties of x (node or
relationship)
toBoolean(x) ➝ converts x (a string, an integer or a boolean) to a boolean value
toFloat(x) ➝ converts x (an integer, floating point or a string value) to a floating point
number toInteger(x) ➝ converts x (a boolean, integer, floating point or a string value)
to an integer type(r) ➝ returns the type of the relationship r
26
Scalar functions
id(x)
properties(x)
27
toInteger(x)
integer to boolean to string to float to
integer integer integer integer
not an
integer
undefined
variable
28
Writing
Queries
MATCH and RETURN
The MATCH keyword is what searches for an existing node, relationship,
label, property, or pattern in the database
MATCH works pretty much like WHERE in SQL
You can find all node labels in the database, search for a particular
node, find all the nodes with a particular relationship, look for patterns of
nodes and relationships, and much more using MATCH
The RETURN keyword specifies what values or results you might
want to return from a Cypher query.
You can tell Cypher to return nodes, relationships, node and
relationship properties, or patterns in your query results
30
Example 1
Find all the actors in the graph
Find all the nodes labeled “Actor” in the
graph
31
Example 2
Find the person with name “Quentin
Tarantino”
32
Example 3
Find when “John Travolta” is
born
You can use
aliases
33
Example 4
Find all the films directed by “Robert
Rodrieguez”
Variable d is not used, so we can avoid to
declare it
34
Example 5
Find all the films directed by “Robert Rodrieguez” or by one of his
friends
35
Example 6
Return any Film where “Quentin Tarantino” is involved by any
means
Any relationship
Duplicate
s
36
Example 7
Return the people that wrote or acted in Four
Rooms
To match on one of multiple types, you can specify this by chaining them together with the pipe
symbol |.
37
Example 8
Return the actors that acted in Pulp Fiction and their role in
the film
38
Example 9
Return the friends of Samuel L.
Jackson
Return the friends of Samuel L. Jackson and the friends of his
friends
Nodes that are a variable number of relationship->node hops away can be found using the following
syntax: - [:TYPE*minHops..maxHops]->. minHops and maxHops are optional and default to 1 and
infinity respectively.
39
Zero-length
path
Using variable length paths that have the lower bound zero means that two variables can point to
the same node.
If the path length between two nodes is zero, they are by definition the same node.
Note that when matching zero length paths the result may contain a match even when
matching on a relationship type not in use.
40
RETURN (expressions)
We can return string concatenation, boolean values, nodes, relationships, paths and
subgraphs
41
OPTIONAL MATCH
OPTIONAL MATCH matches patterns against your graph database,
just
like MATCH does. The difference is that if no matches are found,
OPTIONAL MATCH will use a null for missing parts of the pattern
OPTIONAL MATCH could be considered the Cypher equivalent of the
outer join in SQL
Either the whole pattern is matched, or nothing is matched.
Remember that WHERE is part of the pattern description, and the
predicates will be considered while looking for matches, not after
MATCH PATTERN
OPTIONAL MATCH
PATTERN2 RETURN
42
Example 10
Return the name of the people (:Person) in the DB along with the films they directed
(if any)
43
Example 10
Return the name of the people (:Person) in the DB along with the films they directed
(if any)
44
WITH
The WITH clause allows query parts to be chained together, piping the
results from one to be used as starting points or criteria in the next
It is important to note that WITH affects variables in scope. Any
variables not included in the WITH clause are not carried over to the
rest of the query
One common usage of WITH is to limit the number of entries that are
then passed on to other MATCH clauses. By combining ORDER BY
and LIMIT, it’s possible to get the top X entries by some criteria, and
then bring in additional data from the graph
Another use is to filter on aggregated values. WITH is used to
introduce aggregates which can then be used in predicates in
WHERE
45
Example 11
Find the film title directed or acted by a friend or a friend of a friend of Tarantino (return
only the first friend ordered lexicographically)
Alternative using
WITH
46
Filtering
Matching property values (Example
12)
Find the films released in
1994
Find the films released between 1990 and
1999
48
Test Property Existence (Example
13)
Find all the relationships with a role
property
Alternativ
e
Find all the nodes without a born Alternative (that makes more
property sense)
49
Checking Strings (Example 14)
Find the actors whose name starts with the
letter Q
Find the actors whose name contains the substring
“re”
Find the actors whose name ends with the letter
“a”
50
Using REGEX and Lists (Example 15)
Find the films acted by
Jackson
Find the films released in 1994 and
1995
Check the values in the
list
51
Extension of the example
DB
52
Example 16
Find the films acted by Dawson in which no one of his friends had a role (actor,
director or writer)
Note that we cannot introduce new variables in Path
Expressions
But you can specify the label of the
nodes
53
Example 17
Find the films acted by Travolta and one of his
friends
alternative
…
54
Example 17
Find the films acted by Travolta or one of his friends or
spouses
The function collect() returns a single
aggregated list containing the values
returned by an expression.
With UNWIND, you can transform any list back
into individual rows.
55
Example 18
Find the films acted/directed/written by people whose name starts with the letter ‘J’
or by ‘Rosario Dawson’ and the films acted/directed/written by their spouses
56
Advanced Query
Processing
Aggregations
Cypher has aggregations such as calculating averages, sums,
percentiles, minimum/maximum, and counts
In Cypher, you do not need to specify a grouping key. It implicitly groups
by a non-aggregate field in the return clause
58
Count (Example
19)
There are two different ways you can count return results from your
query.
The first is by using count(n) to count the number of occurrences of n
and does not include null values. You can specify nodes,
relationships, or properties within the parentheses for Cypher to
count.
The second way to count results is with count(*), which counts the
number of unique/distinct result rows returned (including those with null
values).
How many films did Bruce Willis acted in?
59
Counting nulls (Example
20)
Counting null
values
Not counting null
values
60
Collect (Example
21)
The collect() function in Cypher gives you the capability to aggregate
values into a list
You can use this to group a set of values based on a particular starting
node, relationship, property
Find the friends of the people in the DB
61
Collect (Example
21) Find the number of friends for each person in the
DB
length of the
list
62
Pattern comprehension | from Neo4j
4.4+
Pattern comprehension is a syntactic construct available in Cypher for
creating a list based on matchings of a pattern. A pattern comprehension
matches the specified pattern like a normal MATCH clause, with
predicates like a
normal WHERE clause, but yields a custom projection as specified.
https://neo4j.com/docs/cypher-manual/current/syntax/lists/#cypher-pattern-
comprehension
63
Collect (Example 22) DEPRECATED
Find the friends for each person in the DB and the number of
FoaF
Alternative using WITH for intermediate calculations
64
Collect (Example 22) (using pattern
comprehension)
Find the friends for each person in the DB and the number of
FoaF
65
Example 23 DEPRECATED
Find the friends for each person in the DB and the number of FoaF if FoaF are more
than 2
66
Example 23 (using pattern comprehension)
Find the friends for each person in the DB and the number of FoaF if FoaF are more
than 2
67
Example 24 DEPRECATED
Find the friends of Bruce Willis who acted in more than
one film
68
Example 24 (using pattern comprehension)
Find the friends of Bruce Willis who acted in more than
one film
69