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 to create the same node from two different columns and create relationships,

Hi,

I am trying to load data from a csv file with two columns of people and I want to capture the relationship between them if they are on the same row. I cannot figure out how to create the same node even if the person appears in different columns:

|Person| Friend|
|Joe Bloggs| Jane Bloggs|
|Jane Bloggs| John H|

so Jane Bloggs should only appear as one node.

I got to this point but this will create two different nodes for Jane:

LOAD CSV WITH HEADERS FROM "file:///friends.csv" as friends
WITH friends
WHERE friends.Person IS NOT NULL
MERGE (p:person {Name:friends.Person})
MERGE (f:friend {Name:friends.Friend})
MERGE (p)-[:knows]->(f)

1 ACCEPTED SOLUTION

It looks like it should create both nodes because they are MERGEd using different labels, right?

Have you thought about or tried using the :person label for all people? I think that is how I might model this, the relationship would indicate the friend status... Simplistically, something like this

LOAD CSV WITH HEADERS FROM "file:///friends.csv" as friends
WITH friends
WHERE friends.Person IS NOT NULL
MERGE (p:person {Name:friends.Person})
MERGE (f:person {Name:friends.Friend})
MERGE (p)-[:knows]->(f)

View solution in original post

2 REPLIES 2

It looks like it should create both nodes because they are MERGEd using different labels, right?

Have you thought about or tried using the :person label for all people? I think that is how I might model this, the relationship would indicate the friend status... Simplistically, something like this

LOAD CSV WITH HEADERS FROM "file:///friends.csv" as friends
WITH friends
WHERE friends.Person IS NOT NULL
MERGE (p:person {Name:friends.Person})
MERGE (f:person {Name:friends.Friend})
MERGE (p)-[:knows]->(f)

Thank you Joel, that worked!