首页 > 解决方案 > Cypher:嵌套 FOREACH 中的匹配节点

问题描述

我有一个嵌套的 FOREACH 循环,需要将标签与多个标签匹配。

OPTIONAL MATCH (a: Article {URL: event.URL})

FOREACH(ignoreme in case when a is  null then [1] else [] end |
CREATE (a: Article {URL: event.URL})
//other statements ..... //
FOREACH (relation in CASE WHEN event.article.nlp_relations is not null then event.article.nlp_relations else [] end |

         match (a)-[:HAS_NLP_TAG]->(t_from) where (t_from:Tag or t_from:Entity) and t_from.value = relation.from.value
        match (a)-[:HAS_NLP_TAG]->(t_to) where (t_to:Tag or t_to:Entity) and t_to.value = relation.to.value
        call apoc.create.relationship(t_from,relation.type , {}, t_to)
)     
 ) 

这不起作用,因为您不能在 foreach 中使用匹配项。我可以说总会有一个节点要匹配,因为它会在同一个查询的前面创建。所以它永远不会为空,但我不知道如何以目前的形式表达这一点。谁能帮忙

标签: neo4jcypher

解决方案


看起来您可以将您的第二个重新制定FOREACH为正常的MATCH组合CREATE,例如(我没有尝试运行它):

OPTIONAL MATCH (a: Article {URL: event.URL})

FOREACH(ignoreme in case when a is  null then [1] else [] end |
  CREATE (a: Article {URL: event.URL}
)
//other statements ..... //
unwind Case When event.article.nlp_relations is null then [] else event.article.nlp_relations end as relation
match (a)-[:HAS_NLP_TAG]->(t_from) where (t_from:Tag or t_from:Entity) and t_from.value = relation.from.value
match (a)-[:HAS_NLP_TAG]->(t_to) where (t_to:Tag or t_to:Entity) and t_to.value = relation.to.value
call apoc.create.relationship(t_from,relation.type , {}, t_to)
)

WITH您可能仍需要在 -语句之前插入适当的 -MATCH语句。


推荐阅读