Neo4j 101

Cypher

Creating nodes

Using MERGE

If node already exists it wont be duplicated

MERGE (i:Image {
  path: 'rel/path/img.jpeg',
  content_hash: 'unique_123',
  datetime_original: '2001-01-01 00:00:00',
  gps_latitude: 1234.34,
  gps_longitude: 4321.43,
  camera_maker: 'Samsung',
  camera_model: 'S23 Ultra',
  status: 'AVAILABLE | VERIFYING | NOT_FOUND'
})
MERGE (d:Directory {
	path: 'rel/path/gallery_name',
  recursive: true,
  status: 'SCAN_PENDING | SCAN_IN_PROGRESS | SCAN_COMPLETE'
})

Creating relationships

Using MERGE

Pretty similar to create a node, you need the references to both nodes and then

// Explicit relationship direction '->' | '<-'
MERGE (d)-[:CONTAINS]->(i)
                        
// Inferred direction from left to right
// Note: d must be referenced before being used
//   you can use 'MATCH' before 'MERGE'
MERGE (d)-[:CONTAINS]-(i)

RETURN d, i

Optionally, you might to create both nodes along with relationship in a single MERGE. It looks ugly, but it works 🤷🏻‍♂️

MERGE (d:Directory { path: 'abc/def/dir', recursive: true})-[:CONTAINS]-(i:Image { path 'abc/def/dir/random.jpg' })

Updating node properties

Using MATCH and SET

MATCH (d:Directory { path: 'rel/path/gallery' })
SET d.recursive = false

Updating relationship properties

Using MATCH and SET on referenced relationship

MATCH (i:Image)<--[c:CONTAINS]-(d:Directory)
WHERE d.path = 'abc/def/dir'
SET 
  c.since_scan_id = '68mu54hy3twg'
  c.since = '2024-01-01 00:00:00'

Removing nodes

Using MATCH and DETACH DELETE

MATCH (i:Image { path: 'abc/def/dir/portrait1.jpg' })
DETACH DELETE i

Deleting everything in graph. ‼️

MATCH (n) DETACH DELETE n