Shishal
Shishal

Reputation: 27

Execute multiple query based on multiple condition in cypher / apoc

In cypher or APOC, Is there a way to execute multiple query based on multiple condition. I need something similar this APOC

CALL apoc.do.case([condition, query, condition, query, …​], elseQuery:'', 
params:{}) yield value

But here as soon as we met 1st true condition it skip all further condition and query. I want to execute all those query where my condition is true.

In simple word , I am looking for something similar to java case statement (without break; between case)

Update

I ran following query to use multiple apoc.do.when but it seems only my second apoc.do.when is not executing:

CREATE (y:EVENT { _id: 1, localComponentID:'l1', externalComponentID:'e1'}) with y 

call apoc.do.when(exists(y.localComponentID),"MATCH(e:EVENT) where 
e.localComponentID = lcl and e._id <> y._id with  y,e limit 1 create (y)-
[r:LOCAL_LINK]->(e)",'',{y:y,lcl:y.localComponentID}) YIELD value WITH value AS ignored, y

call apoc.do.when(exists(y.externalComponentID),"MATCH(e:EVENT) where 
e.externalComponentID = ext and e._id <> y._id with  y,e limit 1 create (y)-
[r:EXTERNAL_LINK]->(e)",'',{y:y, ext:y.externalComponentID}) YIELD value 
WITH value AS ignored return ignored

If I run above query two time with _id = 1 in first run and _id=2 in second run, I expect two EVENT connected with LOCAL_LINK and EXTERNAL_LINK. But I am only getting LOCAL_LINK between them not the EXTERNAL_LINK. I am not sure what I am doing wrong.

Note : I am using limit 1 because In case of multiple match I just want to create LINK with one node.

Update 2

Got it working , In my sample query I was that not returning y from first apoc.do.when

Here is the updated query which works:

CREATE (y:EVENT { _id: 1, localComponentID:'l1', externalComponentID:'e1'}) with y 

call apoc.do.when(exists(y.localComponentID),"MATCH(e:EVENT) where 
e.localComponentID = lcl and e._id <> y._id with  y,e limit 1 
create (y)-[r:LOCAL_LINK]->(e) RETURN y",'',
{y:y,lcl:y.localComponentID}) YIELD value WITH value AS ignored, y

call apoc.do.when(exists(y.externalComponentID),"MATCH(e:EVENT) where 
e.externalComponentID = ext and e._id <> y._id with  y,e limit 1 
create (y)-[r:EXTERNAL_LINK]->(e)",'',{y:y, ext:y.externalComponentID}) 
YIELD value 
WITH value AS ignored return ignored

Upvotes: 0

Views: 4065

Answers (1)

cybersam
cybersam

Reputation: 67044

You can just call the APOC function apoc.do.when for each condition/query pair (with an empty string as the else argument).

For example:

CALL apoc.do.when(<condition1>, <query1>, '', {}) YIELD value
WITH value AS ignored
CALL apoc.do.when(<condition2>, <query2>, '', {}) YIELD value
WITH value AS ignored
.
.
.

Since your comments indicate your queries are all write-only, the above example assigns the return values to an ignored variable (that you can ignore).

Upvotes: 2

Related Questions