Betelgeuse
Betelgeuse

Reputation: 17

Creating a relationship between two existing nodes in a Neo4j graph using LOAD CSV

I'm trying to create a relationship between two existing nodes: NodeA and NodeB; both nodes have id_name and name as properties. What I am aiming to get is a new relationship between these nodes, for this purpose I have the following 2 files with headers:

file_1.csv

id_name, name
1, aaa
2, bbb
5, ccc

file_2.csv

id_name, name, id_name_1
11, zzz, 2
11, zzz, 1
11, zzz, 5
11, zzz, 5
22, yyy, 1
22, yyy, 2

The goal is using the id_name_1 field from file_2.csv in order to make this relationship between the nodes, but I don't want to add the id_name_1 property to Node2 since I want a node for every id_name, and adding the id_name_1 will create duplicates on id_name. This would be the resulting graph I'm trying to get: enter image description here

At the moment, I tried this but it didn't create any relationship:

LOAD CSV WITH HEADERS FROM "file:///file_2.csv" AS file_2
MATCH (m:NODE2 {id_name_1: toInteger(file_2.id_name_1)})
MATCH (g:NODE1)
WHERE g.id_name = m.id_name_1
MERGE (g)-[r:RELATIONSHIP]->(m)
RETURN *;

What's wrong with this query?

Upvotes: 0

Views: 814

Answers (1)

Betelgeuse
Betelgeuse

Reputation: 17

I just found out what's wrong: the query is attempting to equal the id_name property from Node1 with a property that doesn't exist in Node2, I added another condition as well. This is the good query:

LOAD CSV WITH HEADERS FROM "file:///file_2.csv" AS file_2
MATCH (m:NODE2)
MATCH (g:NODE1)
WHERE g.id_name = toInteger(file_2.id_name_1) AND m.id_name = toInteger(file_2.id_name)
MERGE (g)-[r:RELATIONSHIP]->(m)
RETURN *;

Upvotes: 0

Related Questions