密码与条件APOC程序的动态关系? [英] Cypher dynamic relationship with conditional APOC procedure?
问题描述
我正在学习密码,并试图找到一种在条件满足时创建动态关系的方法.下面是一个例子:
I am learning cypher and trying to find out a way to create dynamic relationship when a condition passes. Here is an example:
我们有一个节点:
(n2)
以及另一个节点与它的子节点具有未知的 TEMP_1 或 TEMP_2 关系:
and another node which has unknown relationship of TEMP_1 or TEMP_2 with its child:
(n1)------[TEMP_1 或 TEMP_2]------>(孩子)
我们想要在 (n2) 和 (child) 之间创建关系(如果存在)
we want to create relationship between (n2) and (child) if it exists
(n2)------[TEMP_1 或 TEMP_2]------>(孩子)
这里是查询:
MATCH (n1: NODE_1)
MATCH (n2: NODE_2)
OPTIONAL MATCH (n1)-[rel:TEMP_1|TEMP_2]->(child)
CALL apoc.do.when(child IS NOT NULL, "CREATE (n2)-[:r]->(ch) RETURN TRUE", "", {n2:n2, r:type(rel), ch:child}) YIELD value
但是,这为我创建了 "r" 类型的关系,而不是从 rel 中获取 type ,因此它看起来如下所示:
However this creates me relationship of type "r" instead of taking type from rel so it looks like following:
(n2)-[r]->(child)
有没有办法在特定条件下创建动态关系?
我有另一个想法,但即使 MATCH 什么都不返回,也需要继续执行查询.
I have another idea but that would require carry on with query execution even if MATCH returns nothing.
MATCH (n1: NODE_1)
MATCH (n2: NODE_2)
MATCH (n1)-[rel:TEMP_1|TEMP_2]->(child)
CALL apoc.do.when(child IS NOT NULL, "CREATE (n2)-[:r]->(ch) RETURN TRUE", "", {n2:n2, r:type(rel), ch:child}) YIELD value
...[other parts of query e.g. MATCH | CREATE ...]
这次没有 OPTIONAL MATCH 并且当 child IS NULL 它返回并且不打扰调用 APOC 过程.但是我想继续查询的其他部分,例如建立其他关系.有没有办法完成这样的事情?
推荐答案
apoc.create.relationship 函数可以创建与动态类型的关系.
The apoc.create.relationship function can create a relationship with a dynamic type.
此外,在引用参数时需要使用$"前缀.
Also, you need to use the "$" prefix when referencing a parameter.
例如:
OPTIONAL MATCH (n1: NODE_1)-[rel:TEMP_1|TEMP_2]->(child)
CALL apoc.do.when(
child IS NOT NULL,
"MATCH (n2: NODE_2) CALL apoc.create.relationship(n2, $r, $ch) YIELD rel RETURN rel",
"",
{r:type(rel), ch:child}) YIELD value
...
此代码段仅在需要时获取 n2
,这假设您稍后在查询中不需要 n2
.
This snippet only gets n2
as needed, which assumes that you do not need n2
later in the query.
这篇关于密码与条件APOC程序的动态关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!