Neo4j learning accelerator

Introduction

Neo4j is a graph database and queried via CYPHER. An Commercial Version incl. the Neo4j-Browser you can download here. Neo4j Enterprise as open source under the AGPLv3 open source license created by the Free software foundation you can find here.

Further Examples are described on Windows platform.

Start “Neo4j Desktop” and just great a new graph. The APOC procedures makes the world much more easy and just has to be downloaded from GitHub here.

Helpful Queries

Playing with e-Mails

Find all nodes, cut the relationships and delete them.

MATCH (n) DETACH DELETE n;

Create a node – relationship – node with properties.

CREATE (m:Instance {name: "Sven"})-[n:SENT {subject: "Likes"}]->(o:Instance {name: "Anna"});

Delete a node where a certain property does not exist.

MATCH (n) WHERE NOT exists(n.name) DETACH DELETE n;

Delete a special constellation (sender/receiver according subject without direction of the mail).

MATCH (m)--(n:SENT)--(o) where n.subject="Hates" DETACH DELETE m,n,o

Import data from an CSV-File (to be stored in the import folder) and create some relations. In the following example and export of Outlook Inbox. The sender/receiver is created with the label “Instance” and the property “name”. The relationship is created with the label “SENT” and the property “subject”

LOAD CSV FROM 'file:///Inbox.csv' as line 
CREATE (:Instance {name: line[1]})-[:SENT {subject: line[0]}]->(:Instance {name: line[2]});

Find all nodes with the label “Instance” with the property “name” and build nodelist that will gone through until there is only one node remaining. Before that all nodes with the same node-property “name” are merged into one.

MATCH (n:Instance) WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count WHERE count > 1
CALL apoc.refactor.mergeNodes(nodelist) YIELD node RETURN node;

Split a node according the string of an property and create multiple nodes out of it with the same relationship (point out every receiver of a mail individually).

MATCH (l)-[m]->(n:Instance) WHERE n.name CONTAINS ";" WITH l,m,n, SPLIT(n.name, ";") as oneInstance
UNWIND RANGE (0,SIZE(oneInstance)-1) as i MERGE (p:Instance {name: l.name})-[q:SENT {subject: m.subject}]->(o:Instance {name: oneInstance[i]}) RETURN p,q,n,o;

Delete nodes that contain a certain substring within the property “name”.

MATCH (n) where n.name CONTAINS ";" DETACH DELETE n;

Finally as one executable block:

MATCH (n) DETACH DELETE n;
LOAD CSV FROM 'file:///Inbox.csv' as line 
CREATE (:Instance {name: line[1]})-[:SENT {subject: line[0]}]->(:Instance {name: line[2]});
MATCH (l)-[m]->(n:Instance) WHERE n.name CONTAINS ";" WITH l,m,n, SPLIT(n.name, ";") as oneInstance
UNWIND RANGE (0,SIZE(oneInstance)-1) as i MERGE (p:Instance {name: l.name})-[q:SENT {subject: m.subject}]->(o:Instance {name: oneInstance[i]}) RETURN p,q,n,o;
MATCH (n:Instance) WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count WHERE count > 1
CALL apoc.refactor.mergeNodes(nodelist) YIELD node RETURN node;
MATCH (n) where n.name CONTAINS ";" DETACH DELETE n;
MATCH (n) WHERE NOT exists(n.name) DETACH DELETE n;