JonoMac
JonoMac

Reputation: 313

Neo4j perform multiple actions within MERGE ON CREATE

In the following code snippet I would like to perform only 1 SET command if MERGE = ON MATCH, and perform the remaining code (SET, UNWIND, MATCH, MERGE) if MERGE = ON CREATE.

However, when I PROFILE the query during an ON MATCH process I can see that the db is being hit with the last 4 lines, the UNWIND, MATCH and MERGE commands. If I comment them out then the db hits reduce down from 6 to 2.

Is there any way to ensure that the last 4 lines are only run during the ON CREATE process, and not run by the ON MATCH process? I've tried silly things like wrapping everything below ON CREATE in brackets or placing a comma between the two etc :-)

The reason for trying to optimise like this is that I have 150k+ json objects and I am trying to speed up the load process as much as possible. I also need to

Thank you!

PROFILE 
CALL apoc.load.json('file:///20220103.json')
//I will be iterating through 150k+ JSON objects here
YIELD value
WHERE value.skills <> []  

MERGE (j:Role {roleId: value.roleId})

ON MATCH
SET j.keep=true 
 
ON CREATE
SET  j.title =replace(value.title,'&amp;','&'), j.createdDate = datetime(), ...... etc

WITH value,j
UNWIND value.skills AS list
MATCH (s:Skill {name: toLower(list.skillname) })
MERGE (j)-[:HAS_SKILL{value:list.rank}]->(s)
 

Upvotes: 0

Views: 296

Answers (1)

I think you can filter out the rows that were just created like this:

WITH value,j
WHERE j.keep IS NULL
UNWIND value.skills AS list

Upvotes: 1

Related Questions