Cypher Snippets
Cypher snippets collected over the years. Some are trivial, some are probably not supported anymore, some depend on APOC or GDS.
Also, with the adoption of or transition to GQL, things will change.
The snippets are mostly for Neo4j but can be useful in other systems supporting Cypher.
Drop a database
Go to the system db and use
Delete duplicate relationships
Delete everything
Truncating data in SQL systems is fast and only challenging when foreign keys are blocking data. With Neo4j truncating data is painful and slow. Shame really.
In v5+
With APOC:
Create a link
Select with empty or different properties
MATCH (n:Composition) WHERE NOT (HAS (n.name)) RETURN n
Create a node
CREATE (p:Person { name:"John Field" }) RETURN p;
match (n) return n limit 100
match (p:Person{firstname:"LEna",lastname:"Pearson"}), (q:Person{firstname:"Swa"})
create (q)-[:Loves]->(p)
match (p:Person) return p limit 100
match (n{id:"/c/en/bird"})-->(other) return other limit 10
match (n{id:"/c/en/bird"})-[t]->(other) return distinct type(t) limit 10
Count
Create link
Unique relationship
Drop all constraints
Louvain method
clear
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
WITH n,r LIMIT 50000
DELETE n,r
RETURN count(n) as deletedNodesCount
or
Export to GraphML
You first need to set the following setting
which can be done from within the manager.
you can find the file in the import dir of the database.
Import GraphML
Import CSV
The csv file has to be in the import directory of the database. Going outside the app is a mess.
The export forma can best be seen from an export of the browser (upper right button set).
Relationships between a collection of nodes
Derived graph
Match (d:Dataset{id: '1e70cf1d-fb4f-41af-902e-6da767b13f4c'})-->(n)
With d+collect(n) as ns
Match p=(u)-[r]->(v) Where u in ns and v in ns
With nodes(p) as A, relationships(p) as B
call apoc.refactor.cloneSubgraph(A, B)
yield input, output
Set output.cloned = true
return output
This one is even better
Match (d:Dataset{id: 'faace384-ef3c-467e-a62b-2cdaf79878b3'})
CALL apoc.path.subgraphAll(d, {relationshipFilter:'Contains>'})
YIELD nodes, relationships
CALL apoc.refactor.cloneSubgraph(nodes, relationships)
YIELD input, output
Set output.derivedFrom = output.id
Set output.id = randomUUID()
With collect(output) as ns
Match (dd:Dataset)-[r:Contains]->(n) Where dd in ns and n in ns
Set r.derivedFrom = r.id
Set r.datasetId = dd.id
Set dd.name = 'Derived'
With ns
Match (u)-[r:Parent]->(v) where u in ns and v in ns
Set r.sourceId = u.id
Set r.targetId = v.id
Set u.parentId = v.id
With ns
Match (u)-[r:GenericLink]->(v) where u in ns and v in ns
Set r.sourceId = u.id
Set r.targetId = v.id
return ns
Auto increment
The non-existent removal is necessary for a lock of some sort…
Random number
Rename relationship label
Import export CSV
You need the following in the conf
dbms.security.procedures.unrestricted=apoc.*
apoc.export.file.enabled=true
apoc.import.file.enabled=true
To export
To import
Get full distinct paths
Import export cypher
CALL apoc.export.cypher.all("all.cypher", {
format: "cypher-shell",
useOptimizations: {type: "UNWIND_BATCH", unwindBatchSize: 20}
})
Can run it
Load dump
Computed properties
Subquery on a path
Match (solution:Sol{name:'Solution 1'})
Match (solution)-[solutionLink:SolutionLink]->(startTask:Task)
Match p=(startTask)-[:ProcessLink*0..50]->(leaf)
WHERE Not (leaf)-->()
With nodes(p) as ns, solution, solutionLink
Call{
With ns
Unwind ns as x
Optional Match (l3{L:'3'})-[:HierarchicalLink*1..7]->(x)
With properties(x{.*, L3:l3.L}) as ex
return collect(ex) as ns2
}
Return ns2, solution.name
// - 3 - 4
// sol - 1 - 2
// - 5 - 6
create (sol:Sol{name:'Solution 1', id: randomUUID()})
create (t1:Task{name:'Task 1', id: randomUUID()})
create (t2:Task{name:'Task 2', id: randomUUID()})
create (t3:Task{name:'Task 3', id: randomUUID()})
create (t4:Task{name:'Task 4', id: randomUUID()})
create (t5:Task{name:'Task 5', id: randomUUID()})
create (t6:Task{name:'Task 6', id: randomUUID()})
create (t1)-[:ProcessLink]->(t2)
create (t2)-[:ProcessLink]->(t3)
create (t3)-[:ProcessLink]->(t4)
create (t2)-[:ProcessLink]->(t5)
create (t5)-[:ProcessLink]->(t6)
create (l3:Box{L:'3'})
create (l3)-[:HierarchicalLink]->(t1)
create (l3)-[:HierarchicalLink]->(t6)
create (sol)-[:SolutionLink{id: randomUUID(), solutionDuration: 23}]->(t1);
// This returns the path and fetches some new properties of the path nodes along the way
Match (solution:Sol{name:'Solution 1'})
Match (solution)-[solutionLink:SolutionLink]->(startTask:Task)
Match p=(startTask)-[:ProcessLink*0..50]->(leaf)
WHERE Not (leaf)-->()
With nodes(p) as ns, solution, solutionLink
Call{
With ns
Unwind ns as x
Optional Match (l3{L:'3'})-[:HierarchicalLink*1..7]->(x)
With properties(x{.*, L3:l3.L}) as ex
return collect(ex) as ns2
}
Return ns2, solution.name
Export part of database
CALL apoc.export.cypher.query(
"MATCH (u:User)
RETURN *",
"users.cypher",
{ format: "cypher-shell", separateFiles: true })
YIELD file, batches, source, format, nodes, relationships, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, time, rows, batchSize;
then copy the script to be executed elsewhere
WITH "match p=(u:Identity)<-[:HAS_IDENTITY]-(v:Device)-[:SIMILAR*1..3]-(w:Device)
where not exists((w)-[:HAS_IDENTITY]->(u))
with u ,w , reduce(t = 1, r IN relationships(p) | t * coalesce(r.strength,1)) as prob
return u.value as Identity, w.id as Device, round(max(prob),1) as Probability
union
match (u:Identity)<-[:HAS_IDENTITY]-(w:Device)
return u.value as Identity, w.id as Device, 1.0 as Probability" AS query
CALL apoc.export.csv.query(query, "predictions.csv", {})
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data;
Show index
This works from v4.4 on and does not require priviledges:
The newer method is:
Create index
To create on all labels:
Change label
Return a prop even if it does not exist
Remove all indexex
Create fulltext index
to query it:
Import CSV with APOC
Epoch to datetime
Convert unix epoch or the date.getTime
in JS.