Neo4j Cheat Sheet
Neo4j Cheat Sheet
Nodes (vertices) typically represent objects, entities or things. Nodes are always
represented by circles.
Relationships (edges) are used to connect nodes. Relationships describe how the
nodes are connected to each other. Relationships are typically verbs.
They are represented by arrows.
1
Neo4j Cheat Sheet
Author: Ali Bińkowska
Labels
A label signify that the node belongs to a subset of nodes within graph. It’s
recommended to use up to 4 labels though unlimited amount of labels is allowed.
On our example graph labels are represented by black rectangle.
Node properties
Properties are key/value pairs and can be added/removed form a node as necessary.
On our example graph they are represented by bubbles.
Relationships direction
Each relationship must have a direction. Although this direction is required, the
relationship can be queried in either direction, or ignored completely at query time.
Relationships are represented by arrows.
Relationship type
Each relationship in a Neo4j graph must have a type. On our example graph
'REQUIRES' indicates the type of relationship.
Relationship properties
As with nodes, relationships can also have properties.
We won't de ine them for this project, though they work the same as properties for
nodes.
Neo4j introduced an option to store vectors in the database. We will use this
functionality and create vector index on top of embeddings that we will store as
nodes' property.
2
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
What is Cypher
3
Neo4j Cheat Sheet
Author: Ali Bińkowska
• Relationships between nodes are written with two dashes, for example (:Skill)--
(:Title)
• The type of the relationship is written using the square brackets between the two
• Properties drawn in a speech bubble are speci ied in a JSON like syntax
4
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Clause De inition
MATCH the clause allows you to specify the patterns Neo4j will search
for in the database. This is the primary way of getting data into
the current set of bindings
MERGE the clause ensures that a pattern exists in the graph. Either the
entire pattern already exists, or the entire pattern needs to be
created.
In this way, it’s helpful to think of MERGE as attempting a
MATCH on the pattern, and if no match is found, a CREATE of
the pattern.
When the speci ied pattern is not present and needs to be
created, any variables previously bound to existing graph
elements will be reused in the pattern. All other elements of
the pattern will be created.
CREATE is used when you want to create a new node or relationship
without checking if it already exists in the graph.
It will always create a new node/relationship, regardless of
whether a similar node/relationship already exists.
SET the clause is used to update labels on nodes and properties on
nodes and relationships.
RETURN the clause de ines the parts of a pattern (nodes, relationships,
and/or properties) to be included in the query result
UNWIND the clause makes it possible to transform any list back into
individual rows
LOAD CSV is used to import data from CSV iles
LIMIT constrains the number of returned rows
WHERE The WHERE clause is not a clause in its own right — rather, it is
part of the MATCH, OPTIONAL MATCH, and WITH clauses
5
f
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Clause De inition
CALL the clause is used to call a procedure deployed in the
database
REMOVE the clause is used to remove properties from nodes and
relationships, and to remove labels from node
DELETE the clause is used to delete nodes, relationships or paths.
EXPLAIN If you want to see the execution plan but not run the query,
prepend your Cypher statement with EXPLAIN. The
statement will always return an empty result and make no
changes to the database.
PROFILE If you want to run the query and see which operators are
doing most of the work, use PROFILE. This will run your query
and keep track of how many rows pass through each operator,
and how much each operator needs to interact with the
storage layer to retrieve the necessary data. Note that pro iling
your query uses more resources, so you should not pro ile
unless you are actively working on a query.
Constrains
MERGE clause per de inition checks for the patter to exists before creating one.
However, if the patter does not already exists, all new elements of the pattern will be
created. This may result in creation of duplicate nodes.
If there is a unique constraint for a node, then an error will be thrown.
6
f
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
The 's' in MATCH (s:Skill) is a variable that you have assigned to each matched Skill
node. It's like a placeholder or a reference to each Skill node found by the MATCH
clause. The RETURN s part then tells Neo4j to return the nodes that s refers to.
Return all nodes with 'Skill' as label and MATCH (s:Skill {name:
property ‘name’ set to ‘Oracle’
'Oracle'})
RETURN s
Query all nodes with 'Skill' as label and MATCH (s:Skill)
property ‘name’ set to ‘Oracle’. Return
WHERE p.name = 'Oracle'
RETURN s.description
only property ‘description’ for all
matching nodes
7
Neo4j Cheat Sheet
Author: Ali Bińkowska
Cypher is very lexible. On some occasions we can skip nodes labels, if the nodes
can be uniquely identi ied by relationships. The same applies to relationship names,
sometime nodes labels indicate which relationship is being implied
Return all Title and Skill nodes that are in MATCH (t:Title)-[:REQUIRES]-
relationship called REQUIRES
>(s:Skill) RETURN t,s
8
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
For Neo4j desktop, the CSV ile has to be imported into the folder designed to store
iles. After that it's accessible via ' ile:/// ile_name.csv'
Load csv ile, return amount of rows. LOAD CSV WITH HEADERS
WITH HEADERS is reserved for CSV iles
FROM 'file:///file_name.csv'
AS row
that have a header. RETURN count(row)
Default ield terminator is ','
The same as above, however we set ield LOAD CSV WITH HEADERS
terminator to ';'
FROM 'file:///file_name.csv'
AS row
FIELDTERMINATOR ';'
RETURN count(row)
Assuming headers consists of columns LOAD CSV WITH HEADERS
['ID','Skill','Title'] we can select which
FROM 'file:///file_name.csv'
AS row
columns we want to return as a result of RETURN row.ID, row.Skill
the query
You can limit the amount of rows that LOAD CSV WITH HEADERS
are presented if you only need to check
FROM file:///file_name.csv'
AS row
if the data is loaded correctly RETURN row
LIMIT 5
9
f
f
f
f
fi
fi
f
f
f
f
f
f
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
10
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Once you create or connect to your irst Neo4j entity it’s worth knowing few
procedures that will tell you abut the schema, relationships and properties
11
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Update a given node property with the given vector in a more space-ef icient way
than directly using SET
db.create.setNodeVectorProperty
Example of creating new property called 'embedding' on each node with label Skill.
New property will be set value from CSV ile from column 'embedding'
LOAD CSV WITH HEADERS FROM 'file:///embeddings.csv'
AS row
MATCH (s:Skill {id: row.Skill})
CALL db.create.setNodeVectorProperty(s, 'embedding',
apoc.convert.fromJsonList(row.embedding))
RETURN count(*)
Create a vector index to search across these embeddings
CALL db.index.vector.createNodeIndex(
indexName :: STRING,
label :: STRING,
propertyKey :: STRING,
vectorDimension :: INTEGER,
vectorSimilarityFunction :: STRING)
Show index
SHOW INDEXES YIELD id, name, type, state,
populationPercent WHERE type = "VECTOR"
Query index
CALL db.index.vector.queryNodes(
indexName :: STRING,
numberOfNearestNeighbours :: INTEGER,
query :: LIST<FLOAT>
) YIELD node, score
12
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Python Driver
1. The scheme used to connect to the Neo4j instance - for example neo4j or
neo4j+s (required)
2. The initial server address for the Neo4j DBMS - for example localhost or
dbhash.databases.neo4j.io (required)
3. The port number that the DBMS is running on (required if the instance is not
running on the default port of 7687)
4. Additional connection con iguration (for example the routing context)
Example:
from neo4j import GraphDatabase
driver = GraphDatabase.driver("neo4j://localhost:7687",
auth=("neo4j", "neo"))
13
f
Neo4j Cheat Sheet
Author: Ali Bińkowska
Follow up commands
Verifying connectivity
driver.verify_connectivity()
Open a new Session
with driver.session() as session:
Or
with driver.session(database="people") as session:
Run a query: auto-commit transactions are a single unit of work that are
immediately executed against the DBMS and acknowledged immediately. You can
run an auto-commit transaction by calling the run() method on the session object,
passing in a Cypher statement as a string and optionally an object containing a set
of parameters.
session.run("MATCH (s:Skill {name: $name}) RETURN s",
name='Oracle')
After executing the query with session.run(query), the method .data() is called on
the result. This method extracts the actual data from the result object into a more
accessible format, usually a list of dictionaries where each dictionary represents a
record returned by the query.
session.run("MATCH (s:Skill {name: $name}) RETURN s",
name='Oracle').data()
Once you are inished with your session, you call the close() method to release any
database connections held by that session.
session.close()
14
f