我掌握了Neo4j桌面的基础知识。这是一个生产案例:我们有工作流程ID。当工作流作业完成时,它会触发另一个作业启动。这就是关系。
我已经定义了300个WF作业,现在我只需创建800个关系。我可以一次做一个。例如,这有效:
MATCH (u) WHERE u.WF_ID = 7
MATCH (v) WHERE v.WF_ID = 8
CREATE (u)-[:TRIGGERS]->(v) return u,v;
但是,如果我采取上述一组命令,复制并粘贴到浏览器或导入文件,我会收到错误。我试过这样:
MATCH (u) WHERE u.WF_ID = 8
MATCH (v) WHERE v.WF_ID = 10
CREATE (u)-[:TRIGGERS]->(v)
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 12
CREATE (u)-[:TRIGGERS]->(v)
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 14
CREATE (u)-[:TRIGGERS]->(v)
return u,v
我得到了:
WITH is required between CREATE and MATCH (line 2, column 1 (offset: 85))
"MATCH (u) WHERE u.WF_ID = 11 MATCH (v) WHERE v.WF_ID = 12 <br>CREATE (u)-[:TRIGGERS]->(v)"
我正在查看关于WITH的Cypher文档,但它并没有帮助我理解我对此案例的需求。
感谢您的帮助!
答案 0 :(得分:1)
WITH也用于将读取与图形更新分开。 查询的每个部分都必须是只读的或只写的。什么时候 从写入部分到读取部分,必须完成切换 带有WITH子句。
https://neo4j.com/docs/developer-manual/current/cypher/clauses/with/#with-introduction
所以你需要在各部分之间添加WITH
(我们需要一个中间变量result
,否则只返回最后一个结果):
MATCH (u) WHERE u.WF_ID = 8
MATCH (v) WHERE v.WF_ID = 10
CREATE (u)-[r:TRIGGERS]->(v)
WITH [[u, v, r]] AS result
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 12
CREATE (u)-[r:TRIGGERS]->(v)
WITH result + [[u, v, r]] AS result
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 14
CREATE (u)-[r:TRIGGERS]->(v)
WITH result + [[u, v, r]] AS result
RETURN result
但是这个查询的变体是丑陋的。由于每个部分的结果未在下一个中使用,我们可以使用UNION
:
MATCH (u) WHERE u.WF_ID = 8
MATCH (v) WHERE v.WF_ID = 10
CREATE (u)-[r:TRIGGERS]->(v)
RETURN u, v, r
UNION
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 12
CREATE (u)-[r:TRIGGERS]->(v)
RETURN u, v, r
UNION
MATCH (u) WHERE u.WF_ID = 11
MATCH (v) WHERE v.WF_ID = 14
CREATE (u)-[r:TRIGGERS]->(v)
RETURN u, v, r
或者我们可以使用UNWIND
:
WITH [[8, 10], [11, 12], [11, 14]] AS pids
UNWIND pids AS ids
MATCH (u) WHERE u.WF_ID = ids[0]
MATCH (v) WHERE v.WF_ID = ids[1]
CREATE (u)-[r:TRIGGERS]->(v)
RETURN u, v, r