Property Graphs: Neo4j and Cypher
Property Graphs: Neo4j and Cypher
1
Neo4j - the most commonly used graph DBMS, by far
https://db-engines.com/en/ranking/graph+dbms
EN: house
3
Modelling translations and complex access rules
house
EN: house
3
Modelling translations and complex access rules
SE: hus
house
DE: Hause
EN: house
ES: casa
3
Modelling translations and complex access rules
SE: hus
house
DE: Hause
EN: house
ES: casa
3
Whiteboard
Friendliness
ACTED_IN ACTED_IN
ACTED_IN
The Matrix
Cloud Atlas
DIRECTED
DIRECTED Lana
Wachowski
Whiteboard friendliness
ACTED_IN ACTED_IN
roles: Bill Smoke roles: Agent Smith
ACTED_IN
roles: Zachry
Movie
Movie title: The Matrix
released: 1999
title: Cloud Atlas
released: 2012
DIRECTED
Person Director
DIRECTED name: Lana Wachowski
born: 1965
Whiteboard friendliness
Intro to the property graph model
Neo4j Fundamentals
• Nodes
• Relationships
• Properties
• Labels
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled
Person Person
Car
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled LOVES
Relationships LOVES
• Relate nodes by type and direction Person Person
LIVES WITH
DR
NS
IV
ES
OW
Car
Property Graph Model Components
name: “Dan”
Nodes born: May 29, 1970 name: “Ann”
twitter: “@dan” born: Dec 5, 1975
• Represent the objects in the graph
• Can be labeled LOVES
Relationships LOVES
• Relate nodes by type and direction Person Person
LIVES WITH
Properties
DR
• Name-value pairs that can go on
NS
IV
ES
OW
nodes and relationships. since:
Jan 10, 2011
brand: “Volvo”
Car model: “V70”
Summary of the graph building blocks
16
Cypher
• Declarative
• Expressive
• Pattern Matching
17
Pattern in our Graph Model
LOVES
Dan Ann
Relationship
NODE NODE
Cypher: Express Graph Patterns
LOVES
Dan Ann
Relationship
Cypher: Express Graph Patterns
LOVES
Dan Ann
Relationship
LOVES
Dan Ann
Relationship
LOVES
Dan Ann
LOVES
Dan Ann
Relationship
Cypher: CREATE Graph Patterns
LOVES
Dan Ann
Relationship
LOVES
Dan Ann
Relationship
LOVES
Dan Ann
LOVES
Dan ?
Relationship
Cypher: MATCH Graph Patterns
LOVES
Dan ?
Relationship
LOVES
Dan ?
Relationship
LOVES
Dan ?
MATCH (person:Person)-[:IS_FRIEND_OF]->(friend),
(friend)-[:LIKES]->(restaurant),
(restaurant)-[:LOCATED_IN]->(loc:Location),
(restaurant)-[:SERVES]->(type:Cuisine)
WHERE person.name = 'Philip'
AND loc.location='New York'
AND type.cuisine='Sushi'
RETURN restaurant.name
The Syntax
Nodes
()
Relationships
-->
-[:DIRECTED]->
Patterns
MATCH (m:Movie)
RETURN m
MATCH and RETURN are Cypher keywords
m is a variable
:Movie is a node label
The components of a Cypher query
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
RETURN p, r, m
MATCH and RETURN are Cypher keywords
p, r, and m are variables
:Movie is a node label
:ACTED_IN is a relationship type
The components of a Cypher query
MATCH (m:Movie)
RETURN m
Graph versus Tabular results
MATCH (m:Movie)
RETURN m.title, m.released
Properties are accessed with {variable}.{property_key}
Case sensitivity
51
1. Application/End-User Goals
A s a n em
ployee
I want to
know wh
company o in the
has simil
ar skills t
So that w o me
e can exc
knowledg hange
e
52
2. Questions to ask of the Domain
A s a n em
ployee
I want to
know wh
company o in the
has simil
ar skills t
So that w o me
e can exc
knowledg hange
e
53
3. Identify Entities
Which people, who work for the same company as me, have
similar skills to me?
• Person
• Company
• Skill
54
4. Identify Relationships Between Entities
Which people, who work for the same company as me, have
similar skills to me?
55
5. Convert to Cypher Paths
56
5. Convert to Cypher Paths
56
5. Convert to Cypher Paths
Node Node
56
5. Convert to Cypher Paths
Node Relationship Node
56
5. Convert to Cypher Paths
Node Relationship Node
• (:Person)-[:WORKS_FOR]->(:Company),
• (:Person)-[:HAS_SKILL]->(:Skill)
56
5. Convert to Cypher Paths
Node Relationship Node
Label Label
• (:Person)-[:WORKS_FOR]->(:Company),
• (:Person)-[:HAS_SKILL]->(:Skill)
Label Label
56
5. Convert to Cypher Paths
Node Relationship Node
• (:Person)-[:WORKS_FOR]->(:Company),
• (:Person)-[:HAS_SKILL]->(:Skill)
Label Relationship Type Label
56
Consolidate Pattern
(:Person)-[:WORKS_FOR]->(:Company),
(:Person)-[:HAS_SKILL]->(:Skill)
(:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)
WORKS_FOR HAS_SKILL
57
Candidate Data Model
(:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)
Company
name:
ACME
R
FO W
O
KS_ RK
WORKS_FOR
R S_
O F
W O
R
HA
IL L
HAS
L
SK
IL
SK
S_S
SK
S_
_SK
S_
S_
HA
KIL
HA
HA
IL L
L
Skill Skill Skill Skill
name: name: name: name:
Scala C# Neo4j Python
58
6. Express Question as Graph Pattern
Which people, who work for the same company as me, have
similar skills to me? Company
company
R
FO W
KS_ O
R RK
O S_
W F O
R
Person Person
me colleague
L
HA IL
S_ SK
SK AS_
IL H
L
Skill
skill
59
Cypher Query
Which people, who work for the same company as me, have similar
skills to me? Company
company
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill)
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) S_
FO
R
W
O
R K RK
WHERE me.name = $name
O S_
W F O
R
collect(skill.name) AS skills HA IL
L
S_ SK
SK S_
ORDER BY score DESC IL
L HA
Skill
skill
60
Cypher Query
Which people, who work for the same company as me, have similar
skills to me? Company
company
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill)
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) S_
FO
R
W
O
R K RK
WHERE me.name = $name
O S_
W F O
R
collect(skill.name) AS skills HA IL
L
S_ SK
SK S_
ORDER BY score DESC IL
L HA
1. Graph pattern
Skill
skill
61
Cypher Query
Which people, who work for the same company as me, have similar
skills to me? Company
company
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill)
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) S_
FO
R
W
O
R K RK
WHERE me.name = $name
O S_
W F O
R
collect(skill.name) AS skills HA IL
L
S_ SK
SK S_
ORDER BY score DESC IL
L HA
1. Graph pattern
Skill
2. Filter, using index if available
skill
62
Cypher Query
Which people, who work for the same company as me, have similar
skills to me? Company
company
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill)
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) S_
FO
R
W
O
R K RK
WHERE me.name = $name
O S_
W F O
R
collect(skill.name) AS skills HA IL
L
S_ SK
SK S_
ORDER BY score DESC IL
L HA
1. Graph pattern
Skill
2. Filter, using index if available
skill
3. Create projection of result
63
First Match
Company
company
Company
OR
W
_F
O
KS
RK
OR
S_
name:
F
W
O
R
ACME
Person Person
colleague
R me
F O W
S_ O
RK
RK
WORKS_FOR
S_
O F
W O
R
HA
S_
IL L
SK
SK
IL L
Person Person Person
S_
HA
Skill
name: name: name:
Tobias Ian Jacob
L
HA KIL skill
S_
SK S_S
IL L HA
IL L
HA
IL L
HAS
L
IL
SK
SK
S_S
SK
_SK
S_
S_
S_
KIL
HA
HA
HA
IL L
L
64
Second Match
Company
company
Company
OR
W
_F
O
KS
RK
OR
S_
name:
F
W
O
R
ACME
Person Person
colleague
R me
F O W
S_ O
RK
RK
WORKS_FOR
S_
O F
W O
R
HA
S_
IL L
SK
SK
IL L
Person Person Person
S_
HA
Skill
name: name: name:
Tobias Ian Jacob
L
HA KIL skill
S_
SK S_S
IL L HA
IL L
HA
IL L
HAS
L
IL
SK
SK
S_S
SK
_SK
S_
S_
S_
KIL
HA
HA
HA
IL L
L
65
Third Match
Company
company
Company
OR
W
_F
O
KS
RK
OR
S_
name:
F
W
O
R
ACME
Person Person
colleague
R me
F O W
S_ O
RK
RK
WORKS_FOR
S_
O F
W O
R
HA
S_
IL L
SK
SK
IL L
Person Person Person
S_
HA
Skill
name: name: name:
Tobias Ian Jacob
L
HA KIL skill
S_
SK S_S
IL L HA
IL L
HA
IL L
HAS
L
IL
SK
SK
S_S
SK
_SK
S_
S_
S_
KIL
HA
HA
HA
IL L
L
66
Result of the Query
+-------------------------------------+
| name | score | skills |
+-------------------------------------+
| "Ian" | 2 | ["Scala","Neo4j"] |
| "Jacob" | 1 | ["Neo4j"] |
+-------------------------------------+
2 rows
67
Modeling exercise: Movie genres
Adding movie genres
vs
Genres as properties
Genres as nodes
MATCH (m1:Movie)-[:IN_GENRE]->(g:Genre),
(m2:Movie)-[:IN_GENRE]->(g)
RETURN m1, m2, g
The (not too) bad side of nodes
OR
Bidirectional Relationships
Use single relationship and ignore direction in queries
• Nadime Francis,
Paolo Guagliardo,
Leonid Libkin
82
Future improvements
Regular Path Queries
85
RDBMSs and Graphs
86
RDBMS can’t handle relationships well
Find all managers and how many people they manage, up to 3 levels down.
Cypher SQL
MATCH (boss)-[:MANAGES*0..3]->(mgr)
WHERE boss.name = "John Doe"
AND (mgr)-[:MANAGES]->()
RETURN mgr.name AS Manager,
size((mgr)-[:MANAGES*1..3]->())
AS Total
Unlocking Value from Your Data Relationships