0

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.

Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
Stetco Oana
  • 101
  • 1
  • 1
  • 11

2 Answers2

1

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); 
Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
  • if I change the MERGE to CREATE, is it still going to do what I want? I would like to create also duplicate relationships. From what I know if I change that it will create me all relationships including duplicates. – Stetco Oana Jul 18 '19 at 09:32
  • I want to make the graph weighted based on duplicate relationships between the nodes – Stetco Oana Jul 18 '19 at 09:32
  • Yeah, it will work. and It will create multiple relationships. Don't forget to delete the relationships created by merge query otherwise, there will be one extra relationship between each pair. – Rajendra Kadam Jul 18 '19 at 09:35
  • perfect! Thank you for the support – Stetco Oana Jul 18 '19 at 09:41
1

The behavior you observed is caused by 2 aspects of the Cypher language:

  1. The WITH clause drops all existing variables except for the ones explicitly specified in the clause. Therefore, since your WITH clause does not specify the n node, n becomes an unbound variable after the clause.

  2. The MERGE clause will create its entire pattern if any part of the pattern does not already exist. Since n is not bound to anything, the MERGE clause would go ahead and create the entire pattern (including the 2 nodes).

So, you could have fixed the issue by simply specifying the n variable in the WITH clause, as in:

WITH n, line.cui= cui

But @Raj's query is even better, avoiding the need for WITH entirely.

cybersam
  • 63,203
  • 6
  • 53
  • 76