We are importing a lot of data from CSV files. Because we want to be able to both create new nodes and "fix" existing nodes (for any particular import). We've come up with the following "pattern".
// Find any name-matched DB Tables
WITH 'NAMING_MATCHING' AS arcBaseName
MATCH (ab:ArcBase {name:arcBaseName})
CALL apoc.cypher.doIt("
MATCH (o:DBTable)
MATCH (d:DBTable {name:o.name}) WHERE id(o)<id(d) // use id() to ensure only one bidirectional link and to stop self-reflection
MERGE (o)-[r:"+ arcBaseName +" {name:o.name+'-"+ab.name+"-'+d.name}]->(d)
ON CREATE
SET r.uuid=randomUUID(),
r.extendedCaption=o.name+'-"+ab.caption+"-'+d.name, r.caption='"+ab.caption+"',
r.originName=o.name, r.originUUID=o.uuid, r.destinationName=d.name, r.destinationUUID=d.uuid
ON MATCH
SET
r.extendedCaption=o.name+'-"+ab.caption+"-'+d.name, r.caption='"+ab.caption+"',
r.originName=o.name, r.originUUID=o.uuid, r.destinationName=d.name, r.destinationUUID=d.uuid
WITH r
RETURN COUNT(r) AS counter
", {}) yield value
RETURN value.counter
;
As you can see, we have ALMOST duplicated the setting of the same set of properties in BOTH the CREATE and MATCH clauses. Is there any way to say something like:
MATCH (d:DBTable {name:o.name}) WHERE id(o)<id(d) // use id() to ensure only one bidirectional link and to stop self-reflection
MERGE (o)-[r:"+ arcBaseName +" {name:o.name+'-"+ab.name+"-'+d.name}]->(d)
SET r.extendedCaption=o.name+'-"+ab.caption+"-'+d.name, r.caption='"+ab.caption+"',
r.originName=o.name, r.originUUID=o.uuid, r.destinationName=d.name, r.destinationUUID=d.uuid
ON CREATE
SET r.uuid=randomUUID()
r.originName=o.name, r.originUUID=o.uuid, r.destinationName=d.name, r.destinationUUID=d.uuid
i.e. set the main set regardless of CREATE or MATCH; only set the additional property on CREATE.
TIA,
Paolo