Can we Create a relationship by matching ID from csv file


Hi All, I have a data in excel sheet and I want to import it into Neo4j. The above pic is just a example not original data. I just want to import data and create relationship based on personId and OrganizationId as per column 5 .
Thanks in advance for your kind response.

Try this; 

After creating the Person and Organization nodes, run your .csv file again to create the relationships based on column 5 values. Here is the Cypher:

with left(line.column5, 2) as n1, right(line.column5, 2) as n2
MATCH (a:Person) where = n1
MATCH (b:Organization) where = n2
with a, b


Thanks for your solution it worked but it will not make relationship between all nodes. One More thing how can i make relation between nodes if I have 3 ids in column 5 e.g. P1.O1.P2

Hello Ameyasoft, The ID will increments on row 11 which mean now we have different ID in column5 as P15O12... So how i can query this ?

Hi @syedsibtainshahbaz, I took the liberty of generating my own csv like your standard. (I can't upload a csv file, so uploaded as txt file, please, rename it to csv file). data.txt (2.5 KB)

match (n) detach delete (n);

USING PERIODIC COMMIT 100 load csv with headers from 'file:///p.csv' as row with 
row where row.person_id is not null 
merge (p:Person {id: row.person_id}) set 
merge (o:Orgaization {id: row.organization_id}) set 
with split(row.relation, 'O')[0] as per, "O"+split(row.relation, 'O')[1] as company 
match (p:Person{id:per}) match(o:Orgaization{id:company})  
merge (p)-[r:LINKS]->(o)  return,,r,,; 

The above query is dynamically get the Person ID and Organization ID without any hard code substrings.

Feel free to change the relationship name and other properties.

and of course, they need index etc, i ignore them for providing the solution.

Let me or the community forums know, if you need further assistance.

Thanks.. :slightly_smiling_face:

Hi Ameyasoft,
Can you please tell me how can I enter relationship properties from csv file. As I have some relationship properties so I need to import it from csv to relationship against the ids. So Can you please guide me what can I do ? Waiting for your kind response . Thanks

You have a column (say column 6) with values and you want to add them as properties to the relationships? Share some sample data and I can help with Cypher queries.

Here is the example of data column 1 and 2 are the seperate ids whereas column 3 is a combine ids.. column 4 and 5 is the relationship properties of column 1 and 2.. column 3 is used to make relationship based on ids.. Hope so you understand.

Hope this is what you are looking for:

MERGE (a:Person {id:line.column1)
MERGE (b:Organization {id:line.column2)
MERGE (a)-[:NEXT {prop:line.column4, id:toInteger(line.column5)}]->(b)

1 Like

I want this type of relationship from csv file after creating nodes and relationship with properties our graph look like this.

Please, refer apoc.create.setRelProperty documentation

Attributes are Key Value pairs.

A small demo

T5,TA5,T5TA5,Very Low,1
USING PERIODIC COMMIT 100 load csv with headers from 'file:///data.csv' as row 
  with row where row.col1 is not null 
 merge (c1:Col1 {id: row.col1}) 
merge (c2:Col2 {id: row.col2}) 
merge (c1)-[r:HAS]->(c2)
with r,c1,c2,row
CALL apoc.create.setRelProperty(r, row.col4, row.col5)
return c1,r,c2;
│"c1"       │"r"             │"c2"        │
│{"id":"T1"}│{"Medium":"4"}  │{"id":"TA1"}│
│{"id":"T2"}│{"High":"5"}    │{"id":"TA2"}│
│{"id":"T3"}│{"Low":"3"}     │{"id":"TA3"}│
│{"id":"T5"}│{"Very Low":"1"}│{"id":"TA5"}│
1 Like

Hi ameyasoft, as above mention example i have load data from csv file and now i want to represent it in hierarchal structure . So can you please guide me how can i do this ?