Search code examples
neo4jcypherload-csv

How to match line of csv which is ignored by constraint and create only relationship


I have been created a graph having a constraint on primary id. In my csv a primary id is duplicate but the other proprieties are different. Based on the other properties I want to create relationships.

I tried multiple times to change the code but it does not do what I need.

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM 'file:///Trial.csv' AS line FIELDTERMINATOR '\t'  
MATCH (n:Trial {id: line.primary_id})  
with line.cui= cui 
MATCH (m:Intervention) 
where m.id = cui 
MERGE (n)-[:HAS_INTERVENTION]->(m); 

I already have the nodes Intervention in the graph as well as the trials. So what I am trying to do is to match a trial with the id from intervention and create only the relationship. Instead is creating me also the nodes.

This is a sample of my data, so the same primary id, having different cuis and I am trying to match on cui:

This is a sample of my data, so the same primary id, having different cuis and I am trying to match on cui.


Solution

  • You can refer the following query which finds Trial and Intervention nodes by primary_id and cui respectively and creates the relationship between them.

    USING PERIODIC COMMIT 
    LOAD CSV WITH HEADERS FROM 'file:///Trial.csv' AS line FIELDTERMINATOR '\t'  
    MATCH (n:Trial {id: line.primary_id}), (m:Intervention {id: line.cui})
    MERGE (n)-[:HAS_INTERVENTION]->(m);