1
votes

In the database I have a label Person which contains nodes with properties: firstname, gender, lastname, id.

I have a CSV file that contains relationship information such as:

id  sibling
1   3,4,5

What I am trying to do is create relationship between the id and the values in the sibling column.

I am using the following code to do so:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'file:///relationship.csv' AS line1
WITH line1, split(line1.sibling, ",") as siblings
UNWIND siblings as sibling
MERGE (p:Person{id:line1.id})
MERGE (k:Person{id:sibling})
MERGE (p)-[:SIBLING_OF]->(k)

but this approach creates new nodes that have only id property, instead of connecting the nodes that already exist in the database.

The weird thing is when I use this query:

MATCH (p:Person)
WHERE p.id='123'
RETURN p

it returns only the node that was previously in the database (containing id, firstname, gender, lastname and id properties), without the newly created relationship, but when I try to return the node with a relationship, such as:

MATCH (p:Person)-[SIBLING_OF]-(k:Person)
WHERE p.id='123
RETURN p,k

the newly created k node gets returned that has only id property.

Does anyone know what I am doing wrong with the query?

1

1 Answers

1
votes

I expect that you have some white space in your CSV file that prevents the cypher from matching the original node.

Ensure you have an constraint or index on :Person(id).

Plus you are merging the id column for every sibling of that user, you could move that up and only do it once.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///relationship.csv' AS line1
MERGE (p:Person {id: trim(line1.id)})
WITH p, line1, split(line1.sibling, ",") as siblings
UNWIND siblings as sibling
MERGE (k:Person {id: trim(sibling)})
MERGE (p)-[:SIBLING_OF]->(k)