2017-03-15 128 views
0

使用實體 - > ENTITY2 - > ENTITY3 - > entity4 - > entity5找關係1到n

開始實體使用實體 我想下面的反應,直到關係其中一個從開始實體連接的實體。

這可能是: -

1) entity1 
2) entity1 -> entity2 
3) entity1 -> entity2 -> entity3 
4) entity1 -> entity2 -> entity3 -> entity4 
5) entity1 -> entity2 -> entity3 -> entity4 -> entity5 

我試着像, 從ENTITY3所有連接。

MATCH (n1:entity1)-[:Relationentitys]->(n2:entity2)- 
    [:Relationentitys]->(n3:entity3)-[:Relationentitys]->(n4:entity4)- 
    [:Relationentitys]->(n5:entity5) where n3.id='5084712c801f' AND n3.tag='entity3' RETURN n1,n2,n3,n4,n5 

關係實體在層次結構中相互連接。

我想不出如何使用密碼。

+0

這是否始終是最多5個實體的鏈而不是樹,其關係分支到您想要的實體之外的其他節點? – InverseFalcon

+0

是的,它是一個實體鏈。 – Vinod

回答

0

如果節點總是形成這些簡單的鏈條,從1到5的實體,沒有分叉到其他節點,那麼你也許可以用一個簡單的可變長度的關係,比賽讓所有的實體節點鏈:

// from the given entity, track back to entity1, the start of the chain 
MATCH (n3:entity3)<-[:Relationentitys*0..4]-(en1:entity1) 
WHERE n3.id='5084712c801f' AND n3.tag='entity3' 
// now get the full chain from entity1 
MATCH (en1)-[:Relationentitys*0..4]->(en) 
RETURN COLLECT(en) as entities 

這應該給你一個鏈中的實體的集合,直到存在於鏈中的許多實體。

+0

感謝您的回覆。 – Vinod

+0

當然。我仍然不完全清楚您的需求。無論您匹配哪個實體,上述都可以工作。如果訂單並不重要,有辦法改善這一點。 – InverseFalcon

+0

是的,我想更多地瞭解這一點,但我仍然無法根據我的回答得到答案。 – Vinod