About Me

My photo
I worked as freelancer on freelance.com for many years, Then opened my Company. I did software coding with my company. But also did pottery -- I know well business management. Software coding is coming next to it as a plus. It helps but organising is as much important as coding. I have many years of experience in business management of Industrial Construction works. I am quite known as company for CAD drafting and 3d for architectural design. I had quite a lot experience at USA and at Turkey. I am less known, in my pottery love.

Wednesday, January 24, 2024

Neo4j Cypher commands - cheat list

  • Cypher 

The source of this text is the PDF user manual that I got from the official site of Neo4j. I felt the need to get down all the essential things, so this cheat sheet has seen surface. 
Happy coding to all  (and I know coding is a love but it makes kind of bizarre happy:)  )

  • Nodes

    • they are represented in parenthesis
    • Our database her have only 2 nodes
      • Person 
        • Label :Person
          • (:Person:Actor
          • (:Person:Director
      • Movie

  • Properties

    • General:
      • they are the key/value pairs
      • Node: Person
        • properties - integer, string or boolean:
          • {name:'Tom Hanks'}
          • {born: 1956}
        • properties can be arrays
          • (:Example {a: 1, b: 3.14})
    • properties of node and relationship
      • The properties for a node with a given label need not to be the same
    • keys()
      • it allows to discover the properties for a node
      • it returns a list of all property keys for a node

  • Relationships

    • No direction
      • (:Person)--(:Movie)
    • Direction of a relationship
      • (:Person)-->(:Movie)
      • (:Person)<--(:Movie)
    • Type of rel.
      • [:ACTED_IN]
    • properties of rel.
      • roles: ['Forrest']
    • partial example: 
      • (m:Movie{title:'Cloud Atlas'})<-[ACTED_IN]-(p:Person)
      • explanation: all people`s all data acted in 'Cloud Atlas'

  • labels: 

    • used to group nodes that have the same general set of properties
    • exemple:
      • (:Person) 
      • (:Movie)

  • How CYPHER works: filtering

    • MATCH: 

      • it retrieve data from the graph
      • it is similar to FROM in SQL

    • To find all people 

      • MATCH (p:Person) RETURN p
      • explanation : here p could be s,x,w,a,b,z or a word. It is defined subjectively

    • Two ways of accessing propery p.born

      • one criteria:
        • MATCH (p:Person{name:'Tom Hanks'}) 
        • RETURN p.born

    • WHERE clause

      • Example: only one criteria
        • MATCH (p:Person)
        • WHERE p.name='Tom Hanks' 
        • RETURN p.born
      • two criteria 
        • WHERE p.name='Tom Hanks' or p.name='Rita Wilson'

    • Query with relationship

      • Query with one of relationship name
        • MATCH (p:Person{name:'Tom Hanks'})-[:ACTED_IN]->(m)
        • RETURN m.title
        • explanation: 
          • because of that we have 2 nodes, m becaomes automatically MOVIE node 
          • if we had 3 nodes we had to specify m:MOVIE
      • Query with another relationship name
        • MATCH (m:Movie{title:'Matrix'})<-[:DIRECTED]->(p:Person)
        • WHERE m.released = 2008 or m.released = 2009
        • RETURN p.name

    • Filtering 

      • using ranges
        • WHERE 2000 <= m.released <= 2003
      • NOT NULL property
        • WHERE p.name='Jack Nicholson' AND m.tagline IS NOT NULL
      • Partial strings
        • WHERE p.name STARTS WITH 'Michael'
      • NOT
        • WHERE NOT exists ((p)-[:DIRECTED]-(m))
        • explanation: the people who never directed a movie
      • RETURN 2 properties of a Node
        • RETURN p.name, m.title
      • Filtering using list-1
        • to compare each property with the values IN the list
        • Note: elements in the list should be same type of data
        • WHERE p.born IN [1965, 1975, 1975]
      • Filtering using list-2
        • compare a value to an existing list
        • WHERE 'Neo' IN r.roles AND m.title='The Matrix'
        • RETURN p.name, r.roles
      • The keys() 
        • for the Person node
          • MATCH (p:Person)
          • RETURN p.name, keys(p)
          • result is:
            • Keanu Reeves
            • ["name", "born"]
          • The result is kind of analysis of database
          • Note: all nodes may not have name and born as properties
          • or may contain null value
        • all the property keys in the graph: name, born, title, released
          • CALL db.property Keys()
          • this will give only the properties without the name of the nodes

    • Writing data to Neo4j

      • MERGE
        • creates nodes and relationship
        • it eliminates duplication
        • MERGE looks up at the primary key before executing, if it is there it will not execute. 
        • Creating nodes
          • Execution
            • MERGE (p:Person{name:'Michael Caine'})
          • Executing multiple MERGE clauses
            • MERGE (p:Person {name:'Katie Holmes'})
            • MERGE (m:Movie {title:'The Dark Knight'})
            • RETURN p.m
        • Creating relationship
          • when you create a relatopnship btw 2 nodes, it must have 
            • type
            • directiom
        • Example:
          • MATCH(p:Person {name:'Michael Caine'})
          • MATCH(m:Movie{title:'The Dark Knight'})
          • MERGE (p)-[ACTED_IN]->(m)
      • CREATE
        • creates nodes and relationships
        • it can create duplication
        • does not look up the primary key

    • Updating Properties

      • Nodes
        • 1st method
          • Adding/Inline as part of the MERGE clause
            • MERGE (p)-[ACTED_IN]{roles:['Alfred Penny']}]->(m)
        • 2nd method SET
          • Using the SET keyword for a reference to a node or a relationship
            • MERGE (p::Person)-[ACTED_IN]->(m:Movie)
            • WHERE p.name="Michael Caine' AND m.title ='The Dark Knight'
            • SET r.roles = ['Alfred Penny']
            • RETURN p,r,m
          • SET - example 2
            • MATCH (m:Movie{title:'Apollo 13'})
            • SET m.released = 1995
          • SET - example 3
            • MATCH (m:Movie {title: 'Apollo 13'})
            • SET m.released = 1996
      • Relationship with SET
        • MATCH (p:Person {name:'Tom Hanks'})
        • MATCH (m:Movie {title:'Forrest Gump'})
        • MERGE (p)-[r:ACTED_IN)->(m)
        • SET r.roles = ['Forrest']

    • Remove a property

      • Relationship
        • Method 1
          • MATCH (p:Person)-[ACTED_IN]->(m:Movie)
          • WHERE p.person = 'Tom Hanks' AND m.title = 'Apollo 13'
          • REMOVE r.roles
      • Node
        • Method 2
          • MATCH (p:Person)
          • WHERE p.name = 'Gene Hackman'
          • SET p.born = null
          • RETURN p
      • Note: The name poperty of Person/a Node is its primary key. This primary key should never be removed

    • Conditionally update or create

      • Example 1 
        • MERGE (p:Person{name:'Robin Williams'})
        • ON MATCH
        • SET p.died = 2014
        • RETURN p
      • Example 2
        • MERGE (m:Movie {title:'Freaky'})
        • ON CREATE
        • SET m.released = 2020
        • ON MATCH
        • SET m.revenue = 15920855
        • RETURN m

    • Delete Nodes and relationships

      • Deleting relationship
        • MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
        • WHERE p.person = 'Tom Hanks' AND m.title = 'Forrest Gump'
        • DELETE r
      • Deleting node
        • Code:
          • MATCH (m:Movie)
          • WHERE m.Title = 'Apollo 13'
          • DELETE m
        • Note: if it has a relationship, it will give an error
        • So use:
          • Code:
            • DETACH DELETE m
          • Note: So this deletes all relationship to a node, then deletes the node
      • Delete all nodes in the graph
        • MATCH(n)
        • DETACH DELETE n
    • Get rid off duplicates (advanced level)
      • refactor code:
        • MATCH (m:Movie)
        • UNWIND m.genres AS genre
        • MERGE (g:Genre {name:genre})
        • MERGE (m)-[IN_GENRE]->(g)
        • SET m.genres = null
      • revised query
        • MATCH (p:ACTOR)-[ACTED_IN]-(m:Movie)--(g:Genre)
        • WHERE p.name = 'Tom Hanks' AND g.name = 'Drama'
        • RETURN m.title AS Movie

    • create Constraint - primary key

      • Node property uniqueness
        • CREATE CONSTRAINT constraint_name FOR (n:Label) REQUIRE n.property IS UNIQUE
        • Example:
          • CREATE CONSTRAINT book_isbn 
            FOR (book:Book) REQUIRE book.isbn IS UNIQUE
        • My Example - Sanatci:
          • CREATE CONSTRAINT sanatci_ad 
            FOR (snt:SANATCI) REQUIRE snt.name IS UNIQUE
          • CREATE CONSTRAINT sanatci_ekol 
            FOR (snt:SANATCIEKL) REQUIRE snt.ekol IS UNIQUE
          • CREATE CONSTRAINT sanatci_ulke 
            FOR (snt:SANATCIULK) REQUIRE snt.ulke IS UNIQUE
          • CREATE CONSTRAINT sanatci_ogrt 
            FOR (snt:SANATCIOGRT) REQUIRE snt.ogretmen IS UNIQUE
          • CREATE CONSTRAINT sanatci_akm 
            FOR (snt:SANATCIAKM) REQUIRE snt.akim IS UNIQUE
        • My Ex - Ekol :
          • CREATE CONSTRAINT ekol 
            FOR (snt:EKOL) REQUIRE snt.name IS UNIQUE
          • CREATE CONSTRAINT akim 
            FOR (snt:AKIM) REQUIRE snt.name IS UNIQUE
      • Relationship uniqueness
        • CREATE CONSTRAINT constraint_name FOR ()-[r:REL_TYPE]-() REQUIRE r.property IS UNIQUE
        • Example :
          • CREATE CONSTRAINT sequels
            FOR ()-[sequel:SEQUEL_OF]-() REQUIRE sequel.order IS UNIQUE
      • Creating index 
        • CREATE INDEX FOR (c:Isletme_dal) ON (c.dalId)

Import from CSV: CSV Structure

Following is taken from: 

The following is the graph in the normal Neptune openCypher load format.

Node file:

:ID, name:String, age:Int, lang:String, :LABEL v1, "marko", 29, , person v2, "lop", , "java", software

Relationship file:

:ID, :START_ID, :END_ID, :TYPE, weight:Double e1, v1, v2, created, 0.4

Source: 



No comments:

Post a Comment

Neo4j and the future

 Time has changed and countries, companies or people not adapting themselves to the changing reality will be loosers. Here let me tell you w...