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

Neo4j Cheat Sheet

Uploaded by

Roche Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Neo4j Cheat Sheet

Uploaded by

Roche Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Neo4j Cheat Sheet

Author: Ali Bińkowska

Introduction to graph database

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.

Neo4j is property graph. Properties provide extra context to the data.

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

Cypher is a query language designed for graphs. Cypher is unique because it


provides a visual way of matching patterns and relationships. Cypher uses an ASCII-
art type of syntax where (nodes)-[:ARE_CONNECTED_TO]->(otherNodes)
using rounded brackets for circular (nodes), and -[:ARROWS]-> for relationships.
When you write a query, you draw a graph pattern through your data

3
Neo4j Cheat Sheet
Author: Ali Bińkowska

Cypher uses following patterns:

• Nodes are represented by parentheses ()

• We use a colon to signify the label(s), for example (s:Skill)

• Relationships between nodes are written with two dashes, for example (:Skill)--

(:Title)

• The direction of a relationship is indicated using a greater than or less than

symbol < or > , for example (:Title)-->(:Skill)

• The type of the relationship is written using the square brackets between the two

dashes: [ and ], for example [:REQUIRES]

• Properties drawn in a speech bubble are speci ied in a JSON like syntax

◦ Properties in Neo4j are key/value pairs, for example {name: ‘Oracle’}

4
f
Neo4j Cheat Sheet
Author: Ali Bińkowska

Selected Cypher clauses

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

Find nodes in Neo4j

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 MATCH (s:Skill)


RETURN s

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

Special MATCH clauses

Disjunction MATCH (n:Skill|Title)


Conjunction MATCH (n:Skill&Certification)
Negation MATCH (n:!Title)
Combo MATCH (n:Skill&!Title)

7
Neo4j Cheat Sheet
Author: Ali Bińkowska

Find nodes with relationships in Neo4j

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

Find all nodes that are connected by MATCH (s)<-[a:REQUIRES]-(t)-


relationships REQUIRES and WORKS_AS.
[b:WORKS_AS]->(r)
RETURN s,a,t,b,r
Return nodes labels and relationships LIMIT 120
names. Limit results to 120

Return all Title and Skill nodes MATCH (t:Title)-[]-(s:Skill)


connected by a relationship. There is is
RETURN t,s
only one relationship between them so
this query is the same as the irst one in
this table

8
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska

Load data from CSV le stored locally (Neo4j Desktop)

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

Load data from CSV le stored remotely (AuraDB)

AuraDB allows to import iles stored remotely on web, GitHub,Google Drive or


Dropbox. While all methods of remote import are described in of icial
documentation <click>, let me mention the GitHub import method.
You just need to navigate to the place that contains the ile and go to the ile. Once
there, you should see a menu bar right above the ile contents.Click on the Raw
button in the button list on the right and copy the url path when the page loads.

9
f
f
f
f
fi
fi
f
f
f
f
f
f
f
f
f
Neo4j Cheat Sheet
Author: Ali Bińkowska

Deleting elements of the graph: DROP, DELETE & REMOVE


In case you want to delete elements of a graph such as nodes, labels, properties or
constraints

Find all nodes with 'Certi ication' label, MATCH (n:Certification)


remove label 'Skill', return nodes names
REMOVE n:Skill RETURN
n.name, labels(n)
and remaining labels
While clause DELETE is used to delete MATCH (n:Certification)
nodes, only nodes without relationship
DETACH DELETE n
can be delete. However you can add
clause DETACH in order to detach
relationships from matching node(s) and
delete them afterwards
Remove relationships, remove all the MATCH (n) DETACH DELETE n
nodes. Useful when you want to start
over
To remove property, we can set it to null MATCH (s:Skill {name:
or we can use REMOVE
'Oracle'})
SET p.id = null
RETURN p.name, p.id
Delete index DROP INDEX skillDescription
IF EXISTS
Delete relationship MATCH ()-[r:REQUIRES]->()
DELETE r
To drop a constraint DROP CONSTRAINT titleUnique
IF EXISTS

10
f
Neo4j Cheat Sheet
Author: Ali Bińkowska

Useful Neo4j procedures

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

Visualises the schema of the call db.schema.visualization


data

Show the derived property call db.schema.nodeTypeProperties


schema of the nodes in
tabular form

Show the derived property call db.schema.relTypeProperties


schema of the relationships in
tabular form

11
f
Neo4j Cheat Sheet
Author: Ali Bińkowska

Vector Search Index

Popularity of vector store in RAG systems is unquestionable. Neo4j has been


enhanced with vector index that works with embeddings that we load into the Neo4j
database. More on search functions can be found under <click>

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

Creating a driver instance where connectionString differs depending whether we


connect AuraDB or Neo4j Desktop

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)

from neo4j import GraphDatabase


driver = GraphDatabase.driver(
connectionString,
auth=(username, password),
**configuration
)

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

You might also like