0% found this document useful (0 votes)
62 views

Property Graphs: Neo4j and Cypher

This document provides an overview of Neo4j and Cypher, which are commonly used for property graph databases and querying. Neo4j is the most popular graph database management system and uses property graphs to model nodes, edges, and properties. Cypher is Neo4j's query language that allows for pattern matching of nodes and relationships in the graph through labels, properties, and variables. The document demonstrates how to create, match, and query graph patterns using Cypher.

Uploaded by

tao hy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Property Graphs: Neo4j and Cypher

This document provides an overview of Neo4j and Cypher, which are commonly used for property graph databases and querying. Neo4j is the most popular graph database management system and uses property graphs to model nodes, edges, and properties. Cypher is Neo4j's query language that allows for pattern matching of nodes and relationships in the graph through labels, properties, and variables. The document demonstrates how to create, match, and query graph patterns using Cypher.

Uploaded by

tao hy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

Property

Graphs: Neo4j and Cypher

1
Neo4j - the most commonly used graph DBMS, by far

https://db-engines.com/en/ranking/graph+dbms

Model: property graphs (graphs with nodes and edges carrying


multiple data values arranged as key-value pairs)

Query language: Cypher.


ASCII-art pattern matching + usual database features
Modelling translations and complex access rules

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

EN: building SE: byggnad


building
ES: edificio DE: Gebäude

SE: hus

house

DE: Hause

EN: house
ES: casa

3
Whiteboard
Friendliness

Easy to design and model, direct representation of the model


Whiteboard friendliness
Whiteboard friendliness

Tom Hanks Hugo Weaving

ACTED_IN ACTED_IN

ACTED_IN
The Matrix
Cloud Atlas
DIRECTED
DIRECTED Lana
Wachowski
Whiteboard friendliness

Person Actor Person Actor

name: Tom Hanks name: Hugo Weaving


born: 1956 born: 1960

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

• Nodes - Entities and complex value types


• Relationships - Connect entities and structure domain
• Properties - Entity attributes, relationship qualities, metadata
• Labels - Group nodes by role
Graph Querying
Why not SQL?

• SQL is inefficient in expressing graph pattern queries




In particular for queries that
• are recursive
• can accept paths of multiple different lengths

• Graph patterns are more intuitive and declarative than joins

• SQL cannot handle path values

16
Cypher

A pattern matching query language made for graphs

• 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

(:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )


Cypher: Express Graph Patterns

LOVES
Dan Ann

Relationship

(:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )

LABEL PROPERTY LABEL PROPERTY


Cypher: Express Graph Patterns

LOVES
Dan Ann

NODE Relationship NODE

(:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )

LABEL PROPERTY LABEL PROPERTY


Cypher: CREATE Graph Patterns

LOVES
Dan Ann

Relationship
Cypher: CREATE Graph Patterns

LOVES
Dan Ann

Relationship

CREATE (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )


Cypher: CREATE Graph Patterns

LOVES
Dan Ann

Relationship

CREATE (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )

LABEL PROPERTY LABEL PROPERTY


Cypher: CREATE Graph Patterns

LOVES
Dan Ann

NODE Relationship NODE

CREATE (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )

LABEL PROPERTY LABEL PROPERTY


Cypher: MATCH Graph Patterns

LOVES
Dan ?

Relationship
Cypher: MATCH Graph Patterns

LOVES
Dan ?

Relationship

MATCH (:Person { name:"Dan"} ) -[:LOVES]-> ( whom ) RETURN whom


Cypher: MATCH Graph Patterns

LOVES
Dan ?

Relationship

MATCH (:Person { name:"Dan"} ) -[:LOVES]-> ( whom ) RETURN whom

LABEL PROPERTY VARIABLE


Cypher: MATCH Graph Patterns

LOVES
Dan ?

NODE Relationship NODE

MATCH (:Person { name:"Dan"} ) -[:LOVES]-> ( whom ) RETURN whom

LABEL PROPERTY VARIABLE


A graph query example
A social recommendation
A social recommendation

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

Nodes are drawn with parentheses.


()
Relationships

Relationships are drawn as arrows, with additional detail in brackets.


-->
-[:DIRECTED]->
Patterns

Patterns are drawn by connecting nodes and relationships with hyphens,


optionally specifying a direction with > and < signs.


()-[]-()
()-[]->()
()<-[]-()
The components of a Cypher query

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 path = (:Person)-[:ACTED_IN]->(:Movie)



RETURN path


MATCH and RETURN are Cypher keywords

path is a variable

:Movie is a node label

:ACTED_IN is a relationship type
Graph versus Tabular results

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

Case sensitive
 Case insensitive




 

Node labels Cypher keywords

Relationship types
Property keys
Case sensitivity

Case sensitive
 Case insensitive




 

:Person MaTcH
:ACTED_IN return
name 

Write queries
The CREATE Clause

CREATE (m:Movie {title:'Mystic River', released:2003})


RETURN m
The SET Clause

MATCH (m:Movie {title: 'Mystic River'})


SET m.tagline = 'We bury our sins here, Dave. We wash them clean.'
RETURN m
The CREATE Clause

MATCH (m:Movie {title: 'Mystic River'})


MATCH (p:Person {name: 'Kevin Bacon'})
CREATE (p)-[r:ACTED_IN {roles: ['Sean']}]->(m)
RETURN p, r, m
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks'})


RETURN p
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks', oscar: true})


RETURN p
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks', oscar: true})


RETURN p


There is not a :Person node with name:'Tom Hanks' and oscar:true in the
graph, but there is a :Person node with name:'Tom Hanks'. 


What do you think will happen here?
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks'})


SET p.oscar = true
RETURN p
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]


->(m:Movie {title: 'The Terminal'})
RETURN p, m
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]


->(m:Movie {title: 'The Terminal'})
RETURN p, m

There is not a :Movie node with title:"The Terminal" in the graph, but
there is a :Person node with name:"Tom Hanks". 


What do you think will happen here?
The MERGE Clause

MERGE (p:Person {name: 'Tom Hanks'})


MERGE (m:Movie {title: 'The Terminal'})
MERGE (p)-[r:ACTED_IN]->(m)
RETURN p, r, m
ON CREATE and ON MATCH

MERGE (p:Person {name: 'Your Name'})


ON CREATE SET p.created = timestamp(), p.updated = 0
ON MATCH SET p.updated = p.updated + 1
RETURN p.created, p.updated;
Graph Modeling
Models
The modeling workflow

1. Derive the 2. Obtain the 3. Develop a 4. Ingest the 5. Query/


question data model data Prove our
model
Developing the model and the query

1.Identify application/end-user goals


2.Figure out what questions to ask of the domain
3.Identify entities in each question
4.Identify relationships between entities in each question
5.Convert entities and relationships to paths
- These become the basis of the data model
6.Express questions as graph patterns
- These become the basis for queries

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

Which people, who work for the same company as


me, have similar skills to me?

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?

• Person WORKS FOR Company


• Person HAS SKILL Skill

55
5. Convert to Cypher Paths

56
5. Convert to Cypher Paths

•Person WORKS FOR Company


•Person HAS SKILL Skill

56
5. Convert to Cypher Paths
Node Node

•Person WORKS FOR Company


•Person HAS SKILL Skill
Node Node

56
5. Convert to Cypher Paths
Node Relationship Node

•Person WORKS FOR Company


•Person HAS SKILL Skill
Node Relationship Node

56
5. Convert to Cypher Paths
Node Relationship Node

•Person WORKS FOR Company


•Person HAS SKILL Skill
Node Relationship Node

• (:Person)-[:WORKS_FOR]->(:Company),
• (:Person)-[:HAS_SKILL]->(:Skill)

56
5. Convert to Cypher Paths
Node Relationship Node

•Person WORKS FOR Company


•Person HAS SKILL Skill
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
Node Relationship Node

Label Relationship Type Label

• (: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)

Company Person 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

Person Person Person


name: name: name:
Tobias Ian Jacob
HA L
S_ KIL
SK
IL L S_S
HA
IL L

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

RETURN colleague.name AS name, Person Person

count(skill) AS score, me colleague

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

RETURN colleague.name AS name, Person Person

count(skill) AS score, me colleague

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

RETURN colleague.name AS name, Person Person

count(skill) AS score, me colleague

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

RETURN colleague.name AS name, Person Person

count(skill) AS score, me colleague

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

Skill Skill Skill Skill


name: name: name: name:
Scala C# Neo4j Python

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

Skill Skill Skill Skill


name: name: name: name:
Scala C# Neo4j Python

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

Skill Skill Skill Skill


name: name: name: name:
Scala C# Neo4j Python

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

The question: should we model them as properties or as nodes?



vs
Genres as properties

MATCH (m:Movie {title: 'The Matrix'})



SET m.genre = ['Action', 'Sci-Fi']

RETURN m
Genres as properties

MATCH (m:Movie {title: 'Mystic River'})



SET m.genre = ['Action', 'Mystery']

RETURN m


The good side of properties

Accessing a movie’s genres is quick and easy.

MATCH (m:Movie {title:"The Matrix"})



RETURN m.genre;
The bad side of properties

Finding movies that share genres is painful and we have a disconnected


pattern in the MATCH clause - a sure sign you have a modeling issue.

MATCH (m1:Movie), (m2:Movie)



WHERE any(x IN m1.genre WHERE x IN m2.genre)

AND m1 <> m2

RETURN m1, m2;
Genres as nodes

MATCH (m:Movie {title:"The Matrix"})



MERGE (action:Genre {name:"Action"})

MERGE (scifi:Genre {name:"Sci-Fi"})

MERGE (m)-[:IN_GENRE]->(action)

MERGE (m)-[:IN_GENRE]->(scifi)



Genres as nodes

MATCH (m:Movie {title:"Mystic River"})



MERGE (action:Genre {name:"Action"})

MERGE (mystery:Genre {name:"Mystery"})

MERGE (m)-[:IN_GENRE]->(action)

MERGE (m)-[:IN_GENRE]->(mystery)
The good side of nodes

Finding movies that share genres is a natural graph pattern.

MATCH (m1:Movie)-[:IN_GENRE]->(g:Genre),

(m2:Movie)-[:IN_GENRE]->(g)

RETURN m1, m2, g
The (not too) bad side of nodes

Accessing the genres of movies requires a bit more typing.

MATCH (m:Movie {title:"The Matrix"}),



(m)-[:IN_GENRE]->(g:Genre)

RETURN g.name;
Symmetric Relationships
Symmetric relationships

OR
Bidirectional Relationships
Use single relationship and ignore direction in queries

MATCH (:Person {name:'Eric'})-[:MARRIED_TO]-(p2)


RETURN p2
Formal semantics

• Nadime Francis,

Paolo Guagliardo,

Leonid Libkin

• Formally defines a (large) core of


Cypher

82
Future improvements
Regular Path Queries

• Conjunctive bi-directional Regular Path Queries with Data


• Plus specification of a cost function for paths,

which allows for more interesting notions of shortest path

PATH PATTERN coauth=()-[:WROTE]->(b)<-[:WROTE]-(),


(b)<-[sale:SELLS]-()
COST min(sale.price)

MATCH (a)-/~coauth* COST x/->(b)


ORDER BY x LIMIT 10
84
Further improvements

• Support for querying from multiple graphs


• Support for returning graphs
• Support for defining views

• Integration with SQL

85
RDBMSs and Graphs

86
RDBMS can’t handle relationships well

• Cannot model or store data and relationships without complexity


• Performance degrades with number and levels of relationships, and
database size
• Query complexity grows with need for JOINs
• Adding new types of data and relationships requires schema
redesign, increasing time to market
Express Complex Queries Easily with Cypher

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

• Model your data naturally as a graph of data and relationships


• Drive graph model from domain and use-cases
• Use relationship information in real-time to transform your business
• Add new relationships on the fly to adapt to your changing
requirements
High Query Performance with a Native Graph DB

• Relationships are first class citizen


• No need for joins, just follow pre-materialized relationships of nodes
• Query & Data-locality – navigate out from your starting points
• Only load what’s needed
• Aggregate and project results as you go
• Optimized disk and memory model for graphs
The performance advantage of materialised relationships

• a sample social graph with ~1,000 persons


• average 50 friends per person
• pathExists(a,b) limited to depth 4
• caches warmed up to eliminate disk I/O
# persons query time
Relational database 1,000 2000ms
Neo4j 1,000 2ms
Neo4j 1,000,000 2ms
Getting started with Neo4j
1. Download Neo4j: http://neo4j.com/download/
2. Start the server.
3. It should be running on: http://localhost:7474
4. Log-in with default credentials 

user: neo4j

password: neo4j
5. Choose a new password
We’re good to go!

You might also like