cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How can I create relationships in neo4j/Cypher from a csv that depend on the type of nodes?

I am trying to import a csv into Neo4j that contains relationships between people, organizations, banks, assets, etc., where there is only one relationship per row. The column names are FROM, A.Type, TO, B.type, and then different properties. Here, the from and to labels have the name, and A-B.type say if it belongs to a person, org., etc. respectibly.

I managed to create the nodes depending on type with FOREACH, like so:

FOREACH (_ IN CASE WHEN line.`A.type` = 'ASSET' THEN [1] ELSE [] END | MERGE (asset:Asset {Name:line.FROM}))
FOREACH (_ IN CASE WHEN line.`A.type` = 'BANK' THEN [1] ELSE [] END | MERGE (bank:Bank {Name: line.FROM}))

.
.
.

FOREACH (_ IN CASE WHEN line.`B.type` = 'ACTIVO' THEN [1] ELSE [] END | MERGE (asset:Asset {Name:line.TO}))
FOREACH (_ IN CASE WHEN line.`B.type` = 'BANCO' THEN [1] ELSE [] END | MERGE (bank:Bank {Name: line.TO}))

.
.
.

My problem now is creating the relationships per row, I've tried many different ways and nothing seems to work.
For Example:

  1. In this case, I changed the FOREACH to two different nodes depending on if they are on the FROM or TO column:
WITH 'link' as line
LOAD CSV WITH HEADERS FROM url AS line
WITH line WHERE line.FROM = 'ASSET' AND line.TO = 'ORGANIZATION'
MERGE (a1:Asset {Name:line.FROM})
MERGE (o2:Organization {Name:line.TO})
CREATE (a1)-[con:PROPERTY_OF]->(o2) 
  1. I also tried a variation of the code for creating nodes:
FOREACH(n IN (CASE WHEN line.`A.type` = 'ASSET' THEN [1] ELSE [] END) |
  FOREACH(t IN CASE WHEN line.`B.type` = 'ORGANIZATION' THEN [1] ELSE [] END |
   MERGE (asset)-[ao:CONNECTED_WITH]->(organization)))
  1. This time I used the APOC library to try generating dinamic relationships depending on the relashionship type:
WITH asset, organization, line
CALL apoc.create.relationship(asset, line.RelationshipType, NULL, organization) YIELD rel
RETURN asset, rel, organization

And different variations of each, creating the nodes from scratch or matching them. Every time the query seems to work, it runs, but it creates no relationships or it creates a single relationship between new nodes that don't exist in the csv, with no name or label.

I am completely new to Cypher/Neo4j and am at my wits end, if someone could point out my mistakes, it would be HIGHLY appreciated.

Thank you in advance!

3 REPLIES 3

omerule
Graph Buddy

Goodevening,
I would create steps to complete the task.
There could also be a better and prefererd way to accomplish your import.

These steps I would take and involves reading the CSV file multiple times:

  1. first create unique nodes with labels and properties
  2. create indexes on the imported nodes labels and propertys
  3. import the CSV again and create the relationship between the existing nodes.

And maybe look at this tutorial:

Yours Kindly
Omer

Is there maybe a link to a more detailed minimal example?

Hey Omer,

Can you help me with following:

https://community.neo4j.com/t/need-help-with-correct-queries/25485/2