Neo4j : Conditional Match,Merge and Delete of relationships

Viewed 508

Need some help with Merge, below is scenario

  • I have two types of node with labels say LEADS and SUBORDINATES and there will be relation b/w them as (LEADS)-[:DEPENDS_ON]->(SUBORDINATE).

  • I am updating LEADS node properties need to update the relationships with SUBORDINATES as well

My update query is as follows ( batching the updates )

UNWIND {updates} as lead
MERGE (l:LEADS {id: lead.id})
ON MATCH  SET l += lead.properties
MATCH (subs:SUBORDINATES) WHERE subs.id IN lead.subordinates
MERGE (l)-[:DEPENDS_ON]->(subs)

I know merge will create new realtionships that are not there, but i need to delete any old relationsships that are not there in the subs.

How do I delete old realtionships that are not merged?

Update #1

I found a way to delete old relationships like below

UNWIND {updates} as lead
MERGE (l:LEADS {id: lead.id})
ON MATCH  SET l += lead.properties
MATCH (subs:SUBORDINATES) WHERE subs.id IN lead.subordinates
MERGE (l)-[r:DEPENDS_ON]->(subs)
ON CREATE SET r.lastUpdated = timestamp();
ON MATCH SET r.lastUpdated = timestamp();
MATCH (l)-[r_old:DEPENDS_ON]->(subs) where r_old.lastUpdated < timestamp();
DELETE r_old

Now the problem is "not to update the relationships" when there is no update to lead.subordinates, i.e. subordiantes are not modifed but some properties of the lead is modified. I need more like conditional merge on relationships where lead.subordiantes is not null

After googling around, came to know that there are no if else like approach in Neo4j,but as a work around foreach with case can be use like below, but my problem is MATCH won't work inside foreach

FOREACH(ignoreMe IN CASE WHEN //Put conditions here//
THEN [1] ELSE [] END |//Put instruction here// )
0 Answers
Related