0% found this document useful (0 votes)
16 views26 pages

Cypher Queries

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)
16 views26 pages

Cypher Queries

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/ 26

Udemy training

Cypher Queries

Ali Bińkowska, Jan 2024


Skill
SpecializedSkill Title

REQUIRES Blockchain
Ethereum Developer

name: ‘Blockchain Developer’


name: ‘Ethereum’

id: 1001

description: ‘Ethereum is …

embedding: […,…,….]
Vector Index
About the data
• File: titles_and_skills.csv
• Columns: Title, Skills

• File: skills_details.csv
• Columns: ID, Skill, Description, Category

• File: embeddings.csv
• Columns: name, embedding /name of the skill and embedding of a description/
Skill
SpecializedSkill File: skills_details.csv Title
Columns: Category

REQUIRES Blockchain
Ethereum Developer
File: titles_and_skills.csv
Columns: Skills
(connection between Titles and
Skills)

name: ‘Blockchain Developer’

name: ‘Ethereum’
File: skills_details.csv File: titles_and_skills.csv
Columns: ID, Skill, Description id: 1001 Columns: Title
description: ‘Ethereum is …
Cypher Queries
Check import of CSV iles

LOAD CSV WITH HEADERS Load csv ile that has headers

from 'https://raw.githubusercontent.com/ Name of the ile to import


MidnightSkyUniverse/udemyNeo4j/master/data/
titles_and_skills.csv'

AS row Assign rows to variable called ‘row’

RETURN row.Title, row.Skills Return data for columns: Title, Skills

LIMIT 15 Limit output to 15 elements


f
f
f
Check import of CSV iles

LOAD CSV WITH HEADERS Load csv ile that has headers

from 'https://raw.githubusercontent.com/ Name of the ile to import

MidnightSkyUniverse/udemyNeo4j/master/data/
skills_details.csv'
Assign rows to variable called ‘row’
AS row
Return data from ile for all columns
RETURN row Limit output to 15 elements

LIMIT 15
f
f
f
f
Create Constraints
CREATE CONSTRAINT skillUnique Create constraint with given name

IF NOT EXISTS If one does not exist yet

FOR (s:Skill) For which node label constraint is created

REQUIRE s.name IS UNIQUE; Condition the constraints is checking

CREATE CONSTRAINT titleUnique


IF NOT EXISTS
FOR (t:Title)
REQUIRE t.name IS UNIQUE;

SHOW ALL CONSTRAINTS


Create ‘Title’ nodes

LOAD CSV WITH HEADERS Load csv ile that has headers

from 'https://raw.githubusercontent.com/ Name of the ile stored in Import folder


MidnightSkyUniverse/udemyNeo4j/master/data/
titles_and_skills.csv'

AS row Assign rows to variable called ‘row’

MERGE (t:Title {name: row.Title}) Create if not existing nodes with property
‘name’ equal to values in column Title
f
f
Create ‘Skill’ nodes
LOAD CSV WITH HEADERS Load csv ile that has headers

from 'https://raw.githubusercontent.com/ Name of the ile stored in Import folder


MidnightSkyUniverse/udemyNeo4j/master/data/skills_details.csv'
AS row Assign rows to variable called ‘row’

MERGE (s:Skill {name: row.Skill}) Create if not existing nodes with ‘name’ property

SET
SET additional properties for each node:
s.id = row.ID, ‘id’ equals ‘ID’ column
‘description’ equals ‘Description’ column
s.description = row.Description, ‘category’ equals ‘Category’ column

s.category=row.Category * property de ined in {} cannot have NULL


values. Properties de ined behind SET can have
NULL values. In a case of NULL value, property
for that node will not be set
f
f
f
f
Add ‘skills’ property to ‘Title’ nodes

LOAD CSV WITH HEADERS Load csv ile that has headers

from 'https://raw.githubusercontent.com/ Name of the ile stored in Import folder


MidnightSkyUniverse/udemyNeo4j/master/data/
titles_and_skills.csv'

AS row Assign rows to variable called ‘row’

Find all nodes which property ‘name’


MATCH (t:Title {name: row.Title}) equals to values from Title column

SET t.skills = split(row.Skills, '|') For nodes you matched, create property ‘skills’
and store there list of strings. We use split()
command to split one string into a list of strings
where ‘|’ is our separator
f
f
Create relationship between Title and Skill

MATCH (t:Title) Find all nodes with ‘Title’ label

UNWIND t.skills AS skill Transform a list of strings into individual strings


and assign them to variable called ‘skill’

MERGE (s:Skill {name: skill}) Find all nodes with label ‘Skill’ which property
‘name’ equals elements stored in variable ‘skill’
MERGE (t)-[:REQUIRES]->(s) Create relationship between matched Title and Skill
nodes

* In case Title ‘Oracle Admin’ required skills ‘Oracle|


SQL’, the result of this clause will be a relationship
between Title ‘Oracle Admin’ and ‘Skill’ nodes with
property ‘name’ equal to ‘Oracle’ and ‘SQL’. Note
that because we use MERGE clause, if such skills
does not exist yet, it will be created
Remove from Title node property ‘skills’

MATCH (t:Title) REMOVE t.skills Remove property from nodes ‘Title’


List unique values in ‘category’ property

MATCH (s:Skill) Find all ‘Skill’ nodes, return unique


values stored in ‘category’ property
RETURN DISTINCT s.category
Set additional labels for ‘Skill’ nodes
MATCH (s:Skill{category:'Certi ication'}) Find all ‘Skill’ nodes, with property ‘category’
equals ‘Certi ication
SET s:Skill:Certi ication Set additional label called ‘Certi ication'

RETURN s.name, labels(s) Return list of matched nodes, property


‘name’ and list of labels

MATCH
(s:Skill{category:'SpecializedSkill'})
SET s:Skill:SpecializedSkill
RETURN s.name, labels(s)

MATCH (s:Skill) REMOVE s.category Remove property ‘category’


f
f
f
f
Match all nodes with relationship ‘REQUIRES’

MATCH (t)-[r:REQUIRES]-(s) Find all nodes that are related by relationship


type ‘REQUIRES’. Return all those nodes and
RETURN t,r,s their relationship

unless I de ine direction of relationship, it’s


from left to right
* the irst command, while returning the same
**************** results will take more resources than the
query where node labels are provided
MATCH (t:Title)-[r:REQUIRES]-(s:Skill)
RETURN t,r,s
f
f
Bonus: pro ile command

PROFILE MATCH (t)-[r:REQUIRES]-(s) Execute two queries using PROFILE


clause (check cheat sheet for
RETURN t,r,s de inition)

The irst query took (on my computer):


* 6507 total db hits
* in 8 ms
*************
And the other query took:
PROFILE MATCH (t:Title)-[r:REQUIRES]-(s:Skill) * 4606 total db hits
* in 4 ms
RETURN t,r,s
f
f
f
Vector Search Index
List Skill nodes and property ‘description’

MATCH (s:Skill) RETURN s.name, Execute search query, return Skills


nodes: property ‘name’ and
s.description ‘description’
Create ‘embedding’ property

LOAD CSV WITH HEADERS Load CSV ile with columns ‘Skill’ and
‘Embedding’ where column
FROM 'https://raw.githubusercontent.com/MidnightSkyUniverse/ ‘Embedding’ contains embeddings of
udemyNeo4j/master/data/embeddings.csv' property ‘description’ of each skill
from our database.
AS row
Search for particular skill among Skill
MATCH (s:Skill {name: row.Skill}) nodes

CALL db.create.setNodeVectorProperty(s, Create ‘embedding’ property with


'embedding', given vector in more space-e icient
way than using SET
apoc.convert.fromJsonList(row.Embedding))
Return number of updated nodes
RETURN count(*)
f
ff
List Skill nodes and property ‘embedding’

MATCH (s:Skill) RETURN s.name, Execute search query, return Skills


nodes: property ‘name’ and
s.embedding ‘embedding’
Create vector index

CALL db.index.vector.createNodeIndex( Create a vector index with


‘SkillDescription’ as its name, add it to

'skillDescription', ‘Skill’ node, use ‘embedding’ property


to search through, set 384* as vector
length, use cosine similarity as search
'Skill', function.
(*) it’s important to know vector length
'embedding', that you have created. OpenAI vector
is 1536 long. For the purpose of the
384, project we used Sentence Transformer
from LangChain which generates
vectors 384 long
'cosine'
)
Show vector index

SHOW INDEXES YIELD id, name, type, state, Check status of the index

populationPercent WHERE type = "VECTOR"


Search newly created index*

CALL db.index.vector.queryNodes( * Please note that since I cannot paste


here 384 elements long vector, we
indexName :: STRING, won’t be using the function until later
when we use Python code to query
database and we can easily pass
numberOfNearestNeighbours :: vectors as arguments

INTEGER,
query :: LIST<FLOAT>
) YIELD node, score
THE END

You might also like