Post on 10-May-2015
Cypher Query Language
Chicago Graph Database Meet-UpMax De Marzi
What is Cypher?
•Graph Query Language for Neo4j
•Aims to make querying simple
Design Decisions
Pattern matching
AA
BB CC
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
ASCII-art patterns
() --> ()
Design Decisions
Directed relationship
(A) --> (B)
AA BB
Design Decisions
Undirected relationship
(A) -- (B)
AA BB
Design Decisions
specific relationships
A -[:LOVES]-> B
AA BBLOVES
Design Decisions
Joined paths
A --> B --> C
AA BB CC
Design Decisions
multiple paths
A --> B --> C, A --> C
AA
BB CC
A --> B --> C <-- A
Design Decisions
Optional relationships
A -[?]-> B
AA BB
Mutating CypherNew in Neo4j 1.8
Create Nodes
The Ninja Turtles
CypherCREATE turtle1 = {name : 'Donatello'}, turtle2 = {name : ‘Michelangelo'}, turtle3 = {name : ‘Raphael'}, turtle4 = {name : ‘Leonardo'}
Create Relationships
The Ninja Turtles are disciples of
Splinter
Cypher
START t1 = node(1), t2 = node(2), t3 = node(3), t4 = node(4)CREATE s = {name: ‘Splinter’ }, t1-[r:DISCIPLE]->s, t2-[r:DISCIPLE]->s, t3-[r:DISCIPLE]->s, t4-[r:DISCIPLE]->s
Delete Nodes
Shredder is defeated
CypherSTART s = node:nodes(name = ‘Shredder’)DELETE s
Update Properties
Donatello uses the Bo StaffMichelangelo is always hungry
CypherSTART t1 = node(0), t2 = node(2)SET t1.weapon = ‘Bo Staff’, t2.hungry = ‘Yes’
Delete Properties
Michelangelo nom nom nom
CypherSTART t2 = node(2)DELETE t2.hungry
Using Relate
The Ninja Turtles love pizza
A pizza node and 4 relationships
are created
CypherSTART turtles = node(1,2,3,4)RELATE turtles-[r:LOVE]->pizza
Using Relate again
The Ninja Turtles love pizza
A pizza node with name “Supreme”
and 4 relationships are created
CypherSTART turtles = node(1,2,3,4)RELATE turtles-[r:LOVE]-(pizza {name:’Supreme’})
Questions?
maxdemarzi.com