2015-09-26 76 views
0

在我的Neo4j應用程序中,我有一個複合節點Decision。每個Decision可以具有無限數量和深度的後裔Decision節點。Neo4j Cypher非共享後代節點

我有一個返回後裔決定的ID一定父決定ID的暗號查詢:

MATCH (d:Decision)-[:CONTAINS*]->(descendantDecision) 
WHERE id(d) = {decisionId} 
RETURN id(descendantDecision) as id 

我需要以此來回報唯一的非共享的後代決定修改該功能節點的ID。

比如我有以下層次:

enter image description here

新的母公司D3{decisionId}必須返回下面的後代決定ID的Cypher查詢:D4D6D7D8

請注意,共享節點的ID爲D7(與D4D8共享)必須返回,因爲此節點只是sh在D3內部層次結構內部,但共享節點的ID爲D5不能返回,因爲它與其他節點(D9)從外部層級共享到D3

所以,以下節點(標有綠色標誌 - D4D6D7D8)必須返回:

enter image description here

請幫我建立這個新的Cypher查詢。

回答

3

這裏的相關標準是結果集的每個成員必須只有來自節點的入站關係是原始節點的後代。因此,語句如下:

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

+0

這是一個純粹的魔力..非常感謝你! – alexanoid

3

這裏是一個替代變種@ 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 
相關問題