2
votes

I'm brand new to Cypher (and Stackoverflow) and am having trouble creating relationships between nodes based on share property keys.

I would like to do something like this:

MATCH (a:Person)-->()<--(b:Country)
WHERE HAS (a.id) AND HAS (b.id) AND a.id=b.id
CREATE (a)-[:LIVES]->(b);

to create a relationship between Country node and Person nodes where they share the same id.

The above creates no errors when run but doesn't create any relationships either and I know that the ids should match.

Many thanks!!

EDIT:

I think I know what is going wrong - I'm asking to match nodes that have a relationship to eachother but no relationships are set up yet hence 0 results. I have now tried:

MATCH (a:Person),
(b:Country)
WHERE HAS (a.id) AND HAS (b.id) AND a.id=b.id
CREATE (a)-[:LIVES]->(b);

and the query is running. It's a big data set so might take a while......

2
try to add some indexes on the keys you are matching, that will speed up the process a lotDmitry

2 Answers

3
votes

That worked. Had to reduce the size of my data set (down from 64k nodes) as Neo4j was taking way too long to process but once I had a smaller set it worked fine.

2
votes

One minor addition for future Googlers.

per the help files as of version 3.4

The has() function has been superseded by exists() and has been removed.

The new code should read

MATCH (a:Person),
(b:Country)
WHERE EXISTS (a.id) AND EXISTS (b.id) AND a.id=b.id
CREATE (a)-[:LIVES]->(b);