2016-08-05 105 views
0

這可能是一種愚蠢的方式。我想創建節點鏈,可能是成千上萬人以下列形式:從一個查詢攜帶變量到另一個查詢以創建帶有密碼的鏈接節點

(n0)-[r0]->(n1)-[r1]->(n2)... 

我已經通過編程生成的暗號這看起來是這樣的:

MERGE (n0:Person)-[r0:RelType]->(n1:Person) 
WITH n1 MERGE (n1:Person)-[r1:RelType]->(n2:Person) 
WITH n2 MERGE (n2:Person)-[r2:RelType]->(n3:Person) 
WITH n3 MERGE (n3:Person)-[r3:RelType]->(n4:Person) 
WITH n4 MERGE (n4:Person)-[r4:RelType]->(n5:Person) 
... 

我然後複製上面的查詢中粘貼Neo4j的Web控制檯就跑,但它給了以下錯誤:

Can't create node `n1` with labels or properties here. The variable is already declared in this context 

我的理解(或者我不?),我們不能用MERGEWITH。另外我知道我們可以批量導入節點,使用Neo4jImport從CSV關係。 但我只是好奇,如果我們可以生成一堆密碼複製粘貼到neo4j Web控制檯並創建所需的圖形。

回答

0

我認爲合併對通過提供的節點N1的關係WITH

MERGE (n0:Person)-[r0:RelType]->(n1:Person) 
WITH n1 MERGE (n1)-[r1:RelType]->(n2:Person) 
WITH n2 MERGE (n2)-[r2:RelType]->(n3:Person) 
WITH n3 MERGE (n3)-[r3:RelType]->(n4:Person) 
WITH n4 MERGE (n4)-[r4:RelType]->(n5:Person) 

(未經測試)

1

@Luanne是在正確的軌道上,但我認爲,當你要刪除的標籤這是你想要的:

CREATE (n1:Person) 
WITH n1 AS n CREATE (n)-[:RelType]->(n1:Person) 
WITH n1 AS n CREATE (n)-[:RelType]->(n1:Person) 
WITH n1 AS n CREATE (n)-[:RelType]->(n1:Person) 
WITH n1 AS n CREATE (n)-[:RelType]->(n1:Person) 

(and so on...) 

除了第一行,所有其他行是相同的。我使用了CREATE,因爲我認爲你根本不想使用MERGE,因爲我相信你正在嘗試創建全新的數據。如果我錯了,你可以使用MERGE

2

如果你想的唯一的事情就是創建節點的長鏈,你可以放鬆的範圍:

CREATE INDEX ON :Person(id) 

UNWIND range(1,100) AS i 
MERGE (p:Person {id: i-1}) 
MERGE (p2:Person {id: i}) 
MERGE (p)-[:RelType]->(p2) 

enter image description here