How to make relationships from a csv on existing nodes and display their values on the edges?

Hi everyone!
I have recently gotten started with Neo4j (desktop, 1.4.5-x86_64) and struggle with making relationships between my nodes. I have the two following .csv files: one describing my nodes, the other describing their relationships.

Nodes.csv: A list of the nodes, single column

Node (header)
Node_1
Node_2
...
Node_n

I loaded and created these nodes. I want to describe their relationships using another .csv ("relationships.csv ") which looks like this:
Node_A|Node_B|Similarity (headers)
Node_k|Node_j| float
...

Where Node_k and Node_j can be any pair of nodes from the Nodes.csv table, and their similarity is a float in the third column.

I have tried the following:

LOAD CSV WITH HEADERS FROM "file:///relationships.csv" as row 
MATCH (n1:Node{id:row.Node_A}) 
MATCH (n2:Node{id:row.Node_B}) 
CREATE (n1)-[:SimilarTo]->(n2);

but get (no changes, no records). I also tried tweaking a bit of this code getting inspired from similar posts but I got errors.

Moreover, I would like to display the float value on the edges of each of my relationships (Similarity column) in the resulting graph.
i.e (Node1)--Similarity(Node_1,Node_2)->(Node2)


Edit

I have finally managed to get the edges I wanted (only using relationships.csv though) with this:

LOAD CSV WITH HEADERS FROM "file:///relationships.csv" as row MERGE(s:Sim{sim:row.Similarity})
MERGE(n1:Column{name:row.Node_A})
MERGE(n2:Column{name:row.Node_B})
CREATE (n1)-[:has_connection]->(n2)

I get the appropriate links in a graph but with "has_connection" on the edges. How can I display the corresponding value for the pair (the third column in the relationships.csv file) in place of "has_connection" ?
Any help would be much appreciated, thanks!

Hello @yoqoo and welcome to the Neo4j community :slight_smile:

Can you share your CSV files?

Regards,
Cobra

First we create the index on nodes:

CREATE INDEX index_entity_name IF NOT EXISTS FOR (n:Entity) ON (n.name)

Then we load nodes:

LOAD CSV WITH HEADERS from "file:///C:/NodeDescription.csv" AS line
WITH DISTINCT line.Entities AS name
MERGE (:Entity {name: name})

Then we load relations:

LOAD CSV WITH HEADERS from "file:///C:/relationships.csv" AS line
WITH line.EntityA AS EntityA, line.EntityB AS EntityB, line.Jaccard AS Jaccard
MATCH (a:Entity {name: EntityA})
MATCH (b:Entity {name: EntityB})
CREATE (a)-[:SIMILAR_TO {score: Jaccard}]->(b)

I do not advise but if you want to get the score as relation name (you will have to use APOC plugin):

LOAD CSV WITH HEADERS from "file:///C:/relationships.csv" AS line
WITH line.EntityA AS EntityA, line.EntityB AS EntityB, line.Jaccard AS Jaccard
MATCH (a:Entity {name: EntityA})
MATCH (b:Entity {name: EntityB})
CALL apoc.create.relationship(a, toString(Jaccard), {score: Jaccard}, b)
YIELD rel
RETURN rel
1 Like

Thanks for the answer. It makes more sense now :slight_smile: !

1 Like