Reputation: 119
I have made a graph database in Neo4j about the Election 2016 In Ireland. I have entered nodes for each Candidate, Constituency and Party.
CREATE(n:Candidate{name:'Gerry Adams', gender:'Male' ,constituency:'Louth', party:'Sinn Fein'});
CREATE(n:Party{party:'Sinn Fein'});
CREATE(n:Constituency{constituency:'Louth'});
All the nodes are in the database, I am trying to get them to link up but I am having problems. This is what I have tried to add relationships:
match (n{constituency:"Louth"}), (c{constituency:"Louth"}) create (n)-[r:FROM]->(c) return n,c;
Where am I going wrong?
Upvotes: 1
Views: 61
Reputation: 11216
Your problem arises because you are not using labels with your identifiers. Without labels, each n and c match everything with a constituency of Louth.
I would match the constituency first and then for each constituency match the candidates for that constituency. Then create relationships for each match. This way you match every object just once.
Something like this...
match (constituency:Constituency {constituency:"Louth"})
with constituency
match (candidate:Candidate {constituency:"Louth"})
create (candidate)-[r:FROM]->(constituency)
return constituency,candidate;
Upvotes: 0