Create Relationships from CSV

Hi,

I need to create relationships between nodes I already created by importing correlation values from a CSV file. The file contains a column named 'sensor' whose values are sensor names (sensor_1, sensor_2...) and the remaining ones contain the correlation value. the file is structured as a square matrix. it would be great to avoid creating self-node relationships. I tried using apoc.do.when but it raises many errors, so I wrote down the following cypher code lines to try to get the relationships at least (self-node ones included), but it does not work, it just returns the sensor nodes. This is my first ever neo4j project and I'm stuck. Can you help me?

LOAD CSV WITH HEADERS FROM 'file:///sensor_corr1.csv' AS row

MATCH (sensor:Sensor {id:toFloat(right(row['sensor'], 1))})

UNWIND [11, 12, 13, 15, 17, 2, 20, 21, 3, 4, 7, 8, 9] AS sensor_id

MATCH (sensor2:Sensor {id:sensor_id})

CALL {

WITH sensor_id, row

MERGE (sensor)-[corr:IS_CORRELATED_WITH {correlation:row['sensor_' + sensor_id]}]->(sensor2)

RETURN corr

}

RETURN sensor, corr

I found the answer by myself. The problem is the position of the "MATCH" clauses, they need to be inside the CALL:

LOAD CSV WITH HEADERS FROM 'file:///sensor_corr1.csv' AS row

UNWIND [11, 12, 13, 15, 17, 2, 20, 21, 3, 4, 7, 8, 9] AS sensor_id

CALL {

WITH sensor_id, row

MATCH (sensor:Sensor {id:toFloat(right(row['sensor'], 1))})

MATCH (sensor2:Sensor {id:sensor_id})

WHERE sensor.id <> sensor2.id

MERGE (sensor)-[corr:IS_CORRELATED_WITH {correlation:row['sensor_' + sensor_id]}]->(sensor2)

RETURN sensor, corr

}

RETURN sensor, corr

With the WHERE clause I avoided the self-node relationship issue too.

You shouldn’t need the call subquery.