Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-17-2021 03:36 AM
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
Node_A|Node_B|Similarity (headers)
Node_k|Node_j| float
...
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)
Solved! Go to Solution.
06-17-2021 06:54 AM
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
06-17-2021 06:23 AM
06-17-2021 06:54 AM
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
06-17-2021 07:09 AM
Thanks for the answer. It makes more sense now !
All the sessions of the conference are now available online