6

Assuming we have a CSV that has nodes with various relationship types. Is there an option to load the CSV in one query allowing each relationship type to be displayed as the relationship name without breaking up the CSV to separate files (one per relationship type)? (We don't want to add the relationship type as a property to the Edge).

Id1 | Id2 | RelationshipType   
1 | 2 | type1  
1 | 3 | type2   
2 | 3 | type1  
... 

We want to later on display & query the data with queries similar to below:

MATCH l=(p:Id1) - [:type1] - (p:Id2) RETURN l;  
MATCH l=(p:Id1) - [:type2] - (p:Id2) RETURN l;
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
skibee
  • 1,279
  • 1
  • 17
  • 37

1 Answers1

7

You can do it using the APOC Procedure apoc.create.relationship.

Considering this CSV file:

Id1|Id2|RelationshipType
1|2|type1
1|3|type2
2|3|type1

The LOAD CSV query will be:

LOAD CSV WITH HEADERS FROM 'file:///sample.csv' AS line FIELDTERMINATOR '|'
WITH line
MERGE(node0:Node {id : line.Id1})
MERGE(node1:Node {id : line.Id2})
WITH node0, node1, line
CALL apoc.create.relationship(node0, line.RelationshipType, {}, node1) YIELD rel
RETURN *

The resultant graph will be:

Resultant Graph

Note: Remember to install APOC procedures according to the version of Neo4j you are using. Take a look in the Version Compatibility Matrix.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89