在我的Neo4j应用程序中,我有一个复合Decision节点。每个Decision可以有无限的后代Decision节点的数量和深度。
我有一个Cypher查询,它返回某个父决策id的子代决策id:
MATCH (d:Decision)-[:CONTAINS*]->(descendantDecision)
WHERE id(d) = {decisionId}
RETURN id(descendantDecision) as id我需要修改这个函数,以便只返回非共享的后代决策节点id。
例如,我有以下层次结构:

对父D3 {decisionId}的新的Cypher查询必须返回以下后代决策id:D4、D6、D7、D8
请注意,必须返回id D7的共享节点(与D4和D8共享),因为该节点仅在D3内部层次结构中共享,但不能返回id D5的共享节点,因为它是从外部层次结构到D3与其他节点(D9)共享的。
因此,必须返回以下节点(标记为绿色标志-- D4、D6、D7、D8):

请帮助我创建这个新的Cypher查询。
发布于 2015-09-26 12:07:08
这里的相关条件是,结果集的每个成员必须只有来自节点的入站关系,而这些关系是原始节点的后代。因此,该声明看起来如下:
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
发布于 2015-09-26 16:26:25
下面是@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 descendanthttps://stackoverflow.com/questions/32796248
复制相似问题