0

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
Shishal
  • 27
  • 1
  • 9
  • Sounds useful, however there are some complications. We can return results easily from this procedure because only one query will be executed as a result. To support multiple queries executing, we'd likely have to restrict all queries to write-only. – InverseFalcon Feb 15 '18 at 22:56
  • Yes, I agree. And in my particular case all queries are write only query. – Shishal Feb 15 '18 at 23:09
  • That could work. Can you add an issue with this request to the APOC GitHub? – InverseFalcon Feb 15 '18 at 23:53
  • created issue on apoc github https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/742 – Shishal Feb 16 '18 at 00:27

1 Answers1

2

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).

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • That could work! And if the queries/conditions were provided in a list, you could match to whatever nodes were needed, unwind the list, and perform all of this with a single `apoc.do.when()` – InverseFalcon Feb 16 '18 at 01:53
  • @InverseFalcon Added example query with multiple apoc.do.when call but I am still not able to get it work. – Shishal Feb 16 '18 at 10:39
  • @cybersam unfortunately no, I found out that in (update 2) if 1st apoc.do.when do not find match to create relationship then 2nd when condition do not work as well. Not sure what I am missing. – Shishal Feb 17 '18 at 12:16
  • not working. returns error ``` Variable `line` not defined (line 24, column 24 (offset: 934)) "call apoc.do.when(trim(line["4Z"])="1","merge (c)-[:USES_PRODUCT{date:line.CreateDt}]->(p4Z)",'',{line:line, c:c, p4Z:p4Z}) YIELD value WITH value AS ignored2" ``` – uma mahesh Apr 04 '18 at 17:06
  • @umamahesh Is that your entire query? As the error states, that query does not define the `line` variable anywhere. If you need more help, then submit another question with complete details. – cybersam Apr 04 '18 at 18:51
  • what i am doing is as below. 1st apoc.do.when happily consumes line, c but 2nd apoc.do.when can't see line, c and breaking the code ``` with line, c call apoc.do.when (...) yield value as ignored1 call apoc.do.when (...) yield value as ignored 2 ... ``` – uma mahesh Apr 23 '18 at 15:11