marcAntoine
marcAntoine

Reputation: 678

Neo4j : create relationship between two or more nodes having the same Label

i have a csv file containing activities (process graph) :

startActivityId,Name,endActivityId
 1,A,2
 2,B,3
 3,C,4
 4,D,5

so that it will look like this : A->B->C->D i imported the csv file successfully into neo4j server : using this Cypher query :

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:path/graph/activity.csv" AS row
CREATE (:Activity {startactivityId:row.startActivityId, Name: row.Name, endActivityId: row.endActivityId});

i then created an index on startactivityId :

CREATE INDEX ON :activity(startActivityId);

then i want to create the relationships between these nodes, so tried this cypher query :

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:path/graph/activity.csv" AS row
MATCH (startActivity:Activity {startActivityId: row.startActivityId})
MATCH (endActivity:Activity {startActivityId: row.endActivityId})
MERGE (startActivity)-[:LINKS_TO]->(endActivity);`

but no relationships created, nothing happens

i'm sure i missed something cause i'm new to cypher but i can't figure it out.

any ideas ?

Upvotes: 1

Views: 1532

Answers (1)

jjaderberg
jjaderberg

Reputation: 9952

I copied your updated csv (and removed the whitespace at the head of the first column) and ran your queries.

neo4j-sh (?)$ USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///Users/jonatan/src/doc/stackexchange/32225817.pdc" as row CREATE (:Activity {startActivityId:row.startActivityId, name:row.Name, endActivityId:row.endActivityId});                                            
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 4
Properties set: 12
Labels added: 4
115 ms

neo4j-sh (?)$ USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///Users/jonatan/src/doc/stackexchange/32225817.pdc" as row MATCH (s:Activity {startActivityId:row.startActivityId}) MATCH (e:Activity {startActivityId:row.endActivityId}) MERGE (s)-[r:LINKS_TO]->(e) RETURN r;
+-------------------+
| r                 |
+-------------------+
| :LINKS_TO[2084]{} |
| :LINKS_TO[2085]{} |
| :LINKS_TO[2086]{} |
+-------------------+
3 rows
Relationships created: 3
178 ms

Three relationships created. To confirm that they are the right relationships I match and return the path (:Activity)-[:LINKS_TO]->().

neo4j-sh (?)$ MATCH p=(:Activity)-[:LINKS_TO]->() RETURN p;         
+-------------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                         |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| [Node[1415]{name:"A",startActivityId:"1",endActivityId:"2"},:LINKS_TO[2084]{},Node[1416]{name:"B",startActivityId:"2",endActivityId:"3"}] |
| [Node[1416]{name:"B",startActivityId:"2",endActivityId:"3"},:LINKS_TO[2085]{},Node[1417]{name:"C",startActivityId:"3",endActivityId:"4"}] |
| [Node[1417]{name:"C",startActivityId:"3",endActivityId:"4"},:LINKS_TO[2086]{},Node[1418]{name:"D",startActivityId:"4",endActivityId:"5"}] |
+-------------------------------------------------------------------------------------------------------------------------------------------+
3 rows
49 ms
neo4j-sh (?)$ 

It looks OK to me, not sure what's not working for you.

What does MATCH p=(:Activity)-[r]->() RETURN p; tell you?

Upvotes: 2

Related Questions