Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-10-2020 08:56 PM
Brand new to Cypher here!
I am trying to write a script that allows me to build relationship between nodes. Currently using the syntax below to make a series of nodes:
MERGE (a : Company {name:'CompanyA'}) ON CREATE SET a = {name:'CompanyA', sector:'Information Technology', lastUpdated: date()} ON MATCH SET a.lastUpdated = date()
And then attempting to establish a relationship between different nodes:
MATCH (a : Company {name:'CompanyA'})
MATCH (b : Industry {name: 'IndustryA'})
MERGE (a)-[r:BELONGS_IN]->(b) ON CREATE SET r = {revenue: '2000', lastUpdated: date()} ON MATCH SET r.lastUpdated = date()
In essence, I am trying to not create duplicate nodes (assuming both the Company and Industry nodes have already been created using the first MERGE command earlier on), instead building the BELONGS_IN relationship using these existing nodes. My problem is that I have a ton of Companies and Industries to update, and currently this syntax only allows me to run one at a time. E.g.
MATCH (a : Company {name:'CompanyA'})
MATCH (b : Industry {name: 'IndustryA'})
MERGE (a)-[r:BELONGS_IN]->(b) ON CREATE SET r = {revenue: '2000', lastUpdated: date()} ON MATCH SET r.lastUpdated = date()
MATCH (c : Company {name:'CompanyC'})
MATCH (d : Industry {name: 'IndustryC'})
MERGE (c)-[r:BELONGS_IN]->(d) ON CREATE SET r = {revenue: '2000', lastUpdated: date()} ON MATCH SET r.lastUpdated = date()
The above, for instance, wouldn't run together. I need to first run the chunk for a and b, then the chunk for c and d.
Is there a syntax that allows me to run all the code blocks in the script in one sitting? Also, if anyone has expertise on how to better format/simplify my syntax, it'd be very much appreciated! Thanks!
Solved! Go to Solution.
11-11-2020 05:37 AM
So you'll want to have all of your company/industry pairings in a CSV file or some external data source, to help you load those in. But yes you can do them all in one go, something like this:
(My syntax may not be perfect, you may need to adjust this is just to give you the idea)
LOAD CSV FROM 'pairings.csv' WITH HEADERS as line
MERGE (a:Company { name: line.company })
MERGE (b:Industry { name: line.industry })
MERGE (a)-[r:BELONGS_IN]->(b)
ON CREATE SET r = { /* whatever */ }
ON MATCH SET r.lastUpdated = date()
Then your CSV file looks like:
company,industry
Google,Technology
Pfizer,Pharmaceuticals
Walmart,Retail
11-11-2020 05:37 AM
So you'll want to have all of your company/industry pairings in a CSV file or some external data source, to help you load those in. But yes you can do them all in one go, something like this:
(My syntax may not be perfect, you may need to adjust this is just to give you the idea)
LOAD CSV FROM 'pairings.csv' WITH HEADERS as line
MERGE (a:Company { name: line.company })
MERGE (b:Industry { name: line.industry })
MERGE (a)-[r:BELONGS_IN]->(b)
ON CREATE SET r = { /* whatever */ }
ON MATCH SET r.lastUpdated = date()
Then your CSV file looks like:
company,industry
Google,Technology
Pfizer,Pharmaceuticals
Walmart,Retail
All the sessions of the conference are now available online