Cypher Query Language
    Chicago Graph Database Meet-Up
             Max De Marzi
What is Cypher?


• Graph Query Language for Neo4j
• Aims to make querying simple
Design Decisions
 Pattern matching


                       A


                   B       C
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

        (A) --> (B)
Design Decisions
 Undirected relationship


             A             B

         (A) -- (B)
Design Decisions
 specific relationships

                   LOVES
             A             B

          A -[:LOVES]-> B
Design Decisions
 Joined paths


      A            B   C

     A --> B --> C
Design Decisions
 multiple paths

                       A


                   B       C

     A --> B --> C, A --> C
      A --> B --> C <-- A
Design Decisions
 Variable length paths
             A           B

         A                   B

  A                              B
             ...
         A -[*]-> B
Design Decisions
 Optional relationships


             A            B

         A -[?]-> B
Mutating Cypher
    New in Neo4j 1.8
Create Nodes
CREATE turtle1 = {name : 'Donatello'},
       turtle2 = {name : ‘Michelangelo'},    Cypher
       turtle3 = {name : ‘Raphael'},
       turtle4 = {name : ‘Leonardo'}




                                        The Ninja Turtles
Create Relationships
START t1 = node(1), t2 = node(2),
     t3 = node(3), t4 = node(4)
CREATE s = {name: ‘Splinter’ },
       t1-[r:DISCIPLE]->s,
                                       Cypher
       t2-[r:DISCIPLE]->s,
       t3-[r:DISCIPLE]->s,
       t4-[r:DISCIPLE]->s




                                    The Ninja Turtles
                                     are disciples of
                                        Splinter
Delete Nodes
START  s = node:nodes(name = ‘Shredder’)
DELETE s                                      Cypher




                                           Shredder is defeated
Update Properties
START t1 = node(0), t2 = node(2)
SET t1.weapon = ‘Bo Staff’, t2.hungry = ‘Yes’   Cypher




                                       Donatello uses the Bo Staff
                                      Michelangelo is always hungry
Delete Properties
START t2 = node(2)
DELETE t2.hungry            Cypher




                     Michelangelo nom nom nom
Using Relate
START turtles = node(1,2,3,4)
RELATE turtles-[r:LOVE]->pizza        Cypher




                                 The Ninja Turtles
                                    love pizza


                                 A pizza node and 4
                                  relationships are
                                       created
Using Relate again
START turtles = node(1,2,3,4)
RELATE turtles-[r:LOVE]-(pizza        Cypher
{name:’Supreme’})



                                 The Ninja Turtles
                                    love pizza


                                 A pizza node with
                                  name “Supreme”
                                 and 4 relationships
                                    are created
Questions?


maxdemarzi.com

Intro to Mutating Cypher

Editor's Notes

  • #3 There existed a number of different ways to query a graph database. This one aims to make querying easy, and to produce queries that are readable. We looked at alternatives - SPARQL, SQL, Gremlin and other...