Neo4j 将变量从一个查询传递到另一个查询以使用密码创建链接节点

Neo4j 将变量从一个查询传递到另一个查询以使用密码创建链接节点,neo4j,cypher,Neo4j,Cypher,这可能是一种愚蠢的做法。我想创建一个节点链,可能有数千个节点,形式如下: (n0)-[r0]->(n1)-[r1]->(n2)... 我已经通过编程生成了cypher,它看起来像这样: MERGE (n0:Person)-[r0:RelType]->(n1:Person) WITH n1 MERGE (n1:Person)-[r1:RelType]->(n2:Person) WITH n2 MERGE (n2:Person)-[r2:RelType]->(n3:P

这可能是一种愚蠢的做法。我想创建一个节点链,可能有数千个节点,形式如下:

(n0)-[r0]->(n1)-[r1]->(n2)...
我已经通过编程生成了cypher,它看起来像这样:

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

我明白(或者我不明白)。我们不能将
合并
内部
一起使用。我还知道,我们可以使用
Neo4jImport
从CSV批量导入节点和关系但我只是好奇我们是否可以生成一组密码,将它们复制粘贴到neo4j web控制台中,并创建所需的图形。

我认为在合并通过WITH提供的节点n1上的关系时,应该删除标签

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)

(未测试)

我认为在合并通过WITH提供的节点n1上的关系时,应该删除标签

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)

(未经测试)

@Luane走上了正确的道路,但我认为这正是你想要的:

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

@luane的做法是正确的,但我认为这正是你想要的:

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

如果您只想创建一个长的节点链,您只需解开一个范围:

CREATE INDEX ON :Person(id)


如果您只想创建一个长的节点链,您只需展开一个范围:

CREATE INDEX ON :Person(id)