在cypher / apoc中基于多个条件执行多个查询

时间:2018-02-15 22:49:28

标签: neo4j cypher neo4j-apoc

在cypher或APOC中,有没有办法根据多个条件执行多个查询。 我需要类似这个APOC的东西

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

但是,只要我们遇到第一个真实条件,它就会跳过所有进一步的条件和查询。我想执行所有那些条件为真的查询。

简单来说,我正在寻找类似于java case语句的东西(没有中断;在case之间)

更新

我运行以下查询以使用多个apoc.do.when,但似乎只有我的第二个apoc.do.when没有执行:

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

如果我在第一次运行中使用_id = 1并且在第二次运行时使用_id = 2运行两次以上查询,我预计两个EVENT与LOCAL_LINK和EXTERNAL_LINK相关联。但我只是在他们之间获得LOCAL_LINK而不是EXTERNAL_LINK。我不确定我做错了什么。

注意:我使用的是限制1,因为在多次匹配的情况下,我只想创建一个节点的LINK。

更新2

搞定了,在我的示例查询中,我没有从第一个 apoc.do.when

返回y

以下是更新后的查询:

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

1 个答案:

答案 0 :(得分:0)

您可以为每个条件/查询对调用APOC函数apoc.do.when(使用空字符串作为var replaceImg = image.split('/appscripts/'); var finalImageName = "../../../appscripts/"+replaceImg[1]; 参数)。

例如:

else

由于您的评论表明您的查询都是只写的,因此上面的示例将返回CALL apoc.do.when(<condition1>, <query1>, '', {}) YIELD value WITH value AS ignored CALL apoc.do.when(<condition2>, <query2>, '', {}) YIELD value WITH value AS ignored . . . 分配给value变量(您可以忽略)。

相关问题