2017-04-24 57 views
0

我期望此查詢生成7個節點與21個關係。UNWIND與子UNWIND生成錯誤節點和關係

它產生13個節點和6間的關係

WITH [{address: "1", connections: []}, 
{address: "2",connections: ["1"]}, 
{address: "3",connections: ["1", "2"]}, 
{address: "4",connections: ["1", "2", "3"]}, 
{address: "5",connections: ["1", "2", "3", "4"]}, 
{address: "6",connections: ["1", "2", "3", "4", "5"]}, 
{address: "7",connections: ["1", "2", "3", "4", "5", "6"]}] AS seeds 
UNWIND seeds AS seed 
MERGE (source:Address { address: seed.address }) 

WITH seed.connections AS connections 
UNWIND connections AS connection 
MATCH (target:Address) WHERE target.address = connection 

MERGE (source)-[:CONNECTS_TO]->(target) 

我試圖十幾+變化,包括FOREACH。

我錯了,認爲這是可能的嗎?

回答

1

這是非常接近的,有一件事我們需要糾正這個查詢的預期工作:您的source變量的範圍。這超出範圍在查詢中對與中間:

... 
WITH seed.connections AS connections 
... 

source不包含在WITH,所以它超出範圍。您的MERGE最後一行中的source未綁定到任何先前節點,因此在某個節點和目標之間不存在此類模式的情況下,將創建一個新的空白節點作爲該關係的起始節點。

爲了解決這個問題,我們需要包括source在您的有,使其保持在範圍:

... 
WITH source, seed.connections AS connections 
... 
+0

謝謝你的回答!將立即測試。 – Geoffrey

+0

100%完美。我嘗試傳遞其他東西,比如'seed.address',顯然,找不到魔法。謝謝! – Geoffrey

0

由於@InverseFalcon介紹,最終的查詢看起來像這樣

WITH [{address: "1", connections: []}, 
{address: "2",connections: ["1"]}, 
{address: "3",connections: ["1", "2"]}, 
{address: "4",connections: ["1", "2", "3"]}, 
{address: "5",connections: ["1", "2", "3", "4"]}, 
{address: "6",connections: ["1", "2", "3", "4", "5"]}, 
{address: "7",connections: ["1", "2", "3", "4", "5", "6"]}] AS seeds 
UNWIND seeds AS seed 
MERGE (source:Address { address: seed.address }) 

WITH source, seed.connections AS connections 
UNWIND connections AS connection 
MATCH (target:Address) WHERE target.address = connection 

MERGE (source)-[:CONNECTS_TO]->(target)