如何在 Cypher 中使用一条语句创建所有边?
How to create all edges that using one statement in Cypher?
如何在 Cypher 中使用一条语句创建所有边?
例如:假设我有一个这样的对象
Employees {name: "abc, country: "NZ", zipcode: "123456"}
Employees {name: "def", country: "AUS", zipcode: "964573"}
假设我有以下 Manager 对象
Manager { name: "abc", depatment: "product"}
Manager {name: "abc", depatment: "sales"}
Manager {name: "abc", depatment: "marketing"}
最后是地址对象
Address {zipcode: "964573", street: "Auckland St"}
现在我想在 Employees.name = Manager.name and Employees.zipcode = Address.zipcode
处创建所有边,但是如果 Employees.name != Manager.name
但 Employees.zipcode = Address.zipcode
则我希望在 Employees
和 [= 之间创建所有边21=] 同样,如果 Employees.zipcode != Address.zipcode
但 Employees.name = Manager.name
那么我希望在 Employees
和 Manager
之间创建所有边。我想一次实现所有这些 statement/query
简单地说,如果 Employees、Manager 和 Address 之间存在匹配的顶点,我希望在它们之间创建所有边,但如果任意两个之间只有匹配,我希望在这两个顶点之间也创建边.我想在一个 query/statement 中完成所有这些?
是否可以在一条语句中写出满足以上所有条件的查询?
到目前为止我尝试的是这个
首先使用 MATCH 子句找到对,然后在它们之间创建关系。
MATCH (e:Employees),(m:Manager), (a:Address)
WHERE e.name=m.name or e.zipcode = a.zipcode
WITH e,m,a
CREATE (m)-[:REL_NAME]->(e), (e)-[:ADDR_REL]->(a)
由于 Where
子句,这显然行不通,因为如果 e.name=m.name
则不会检查 e.zipcode = a.zipcode
,因此不会在员工和地址之间创建边缘。
以下查询避免为 :Manager(name)
和 :Address(zipcode)
生成 of all 3 node labels (and will perform better if you have indexes):
MATCH (e:Employees)
OPTIONAL MATCH (m:Manager)
WHERE e.name = m.name
WITH e, COLLECT(m) AS mList
FOREACH(x IN mList | CREATE (x)-[:REL_NAME]->(e))
WITH e
OPTIONAL MATCH (a:Address)
WHERE e.zipcode = a.zipcode
WITH e, COLLECT(a) AS aList
FOREACH(y IN aList | CREATE (e)-[:ADDR_REL]->(y))
如何在 Cypher 中使用一条语句创建所有边?
例如:假设我有一个这样的对象
Employees {name: "abc, country: "NZ", zipcode: "123456"}
Employees {name: "def", country: "AUS", zipcode: "964573"}
假设我有以下 Manager 对象
Manager { name: "abc", depatment: "product"}
Manager {name: "abc", depatment: "sales"}
Manager {name: "abc", depatment: "marketing"}
最后是地址对象
Address {zipcode: "964573", street: "Auckland St"}
现在我想在 Employees.name = Manager.name and Employees.zipcode = Address.zipcode
处创建所有边,但是如果 Employees.name != Manager.name
但 Employees.zipcode = Address.zipcode
则我希望在 Employees
和 [= 之间创建所有边21=] 同样,如果 Employees.zipcode != Address.zipcode
但 Employees.name = Manager.name
那么我希望在 Employees
和 Manager
之间创建所有边。我想一次实现所有这些 statement/query
简单地说,如果 Employees、Manager 和 Address 之间存在匹配的顶点,我希望在它们之间创建所有边,但如果任意两个之间只有匹配,我希望在这两个顶点之间也创建边.我想在一个 query/statement 中完成所有这些?
是否可以在一条语句中写出满足以上所有条件的查询?
到目前为止我尝试的是这个
首先使用 MATCH 子句找到对,然后在它们之间创建关系。
MATCH (e:Employees),(m:Manager), (a:Address)
WHERE e.name=m.name or e.zipcode = a.zipcode
WITH e,m,a
CREATE (m)-[:REL_NAME]->(e), (e)-[:ADDR_REL]->(a)
由于 Where
子句,这显然行不通,因为如果 e.name=m.name
则不会检查 e.zipcode = a.zipcode
,因此不会在员工和地址之间创建边缘。
以下查询避免为 :Manager(name)
和 :Address(zipcode)
生成
MATCH (e:Employees)
OPTIONAL MATCH (m:Manager)
WHERE e.name = m.name
WITH e, COLLECT(m) AS mList
FOREACH(x IN mList | CREATE (x)-[:REL_NAME]->(e))
WITH e
OPTIONAL MATCH (a:Address)
WHERE e.zipcode = a.zipcode
WITH e, COLLECT(a) AS aList
FOREACH(y IN aList | CREATE (e)-[:ADDR_REL]->(y))