Neo4j Cypher非共享后代节点
问题描述:
在我的Neo4j应用程序中,我有一个复合节点Decision
。每个Decision
可以具有无限数量和深度的后裔Decision
节点。Neo4j Cypher非共享后代节点
我有一个返回后裔决定的ID一定父决定ID的暗号查询:
MATCH (d:Decision)-[:CONTAINS*]->(descendantDecision)
WHERE id(d) = {decisionId}
RETURN id(descendantDecision) as id
我需要以此来回报唯一的非共享的后代决定修改该功能节点的ID。
比如我有以下层次:
新的母公司D3
{decisionId}
必须返回下面的后代决定ID的Cypher查询:D4
,D6
,D7
,D8
请注意,共享节点的ID为D7
(与D4
和D8
共享)必须返回,因为此节点只是sh在D3
内部层次结构内部,但共享节点的ID为D5
不能返回,因为它与其他节点(D9
)从外部层级共享到D3
。
所以,以下节点(标有绿色标志 - D4
,D6
,D7
,D8
)必须返回:
请帮我建立这个新的Cypher查询。
答
这里的相关标准是结果集的每个成员必须只有来自节点的入站关系是原始节点的后代。因此,语句如下:
MATCH (d:Decision)-[:CONTAINS*0..]->(descendantDecision)
WHERE d.name=3
WITH collect(descendantDecision) AS allDescendants
UNWIND allDescendants AS descendant
MATCH (descendant)<-[:CONTAINS]-(parent)
WITH descendant, collect(parent) AS parents, allDescendants
WHERE ALL (x IN parents WHERE x IN allDescendants) // check if all inbound start at a descendant
RETURN descendant
为了完整性,我创建了这个Neo4j的控制台设置:http://console.neo4j.org/?id=ufyu0u
答
这里是一个替代变种@ sarmbruster的建议:
MATCH (d:Decision)-[:CONTAINS*0..]->(descendantDecision)
WHERE d.name=3
WITH collect(DISTINCT descendantDecision) AS allDescendants
UNWIND allDescendants AS descendant
WITH allDescendants, descendant
WHERE ALL (p IN (descendant)<-[:CONTAINS]-()
WHERE last(nodes(p)) IN allDescendants)
RETURN descendant
这是一个纯粹的魔力..非常感谢你! – alexanoid