Cypher:过滤掉具有特定路径的节点
Cypher: filter out nodes with certain paths
假设:
- 我的 Neo4j 数据库中有
SpecialFromLabel
个节点。
- 这些节点连接到
SpecialToLabel
个具有 SPECIAL_REL
关系的节点
- 因此,存在以下路径:
p = (from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
- 具有
SpecialToLabel
的节点有一个 属性 to_prop
可以有不同的字符串值
我如何构造一个查询,为我提供具有路径的所有 from
个节点:p=(from: SpecialFromLabel)-[:SPECIAL_REL]->(to: SpecialToLabel)
- 但 那些 from
个节点没有通往 to
个节点的路径,其中 to.to_prop
在不需要的值集中?
在伪代码中(不可运行)我想要这样的东西:
MATCH (unwanted_from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE (to.`to_prop` in $UnwantedValues)
with unwanted_from
MATCH (from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE from not in unwanted_from
RETURN from
我会这样处理
// collect the unwanted endpoints
MATCH (to: SpecialToLabel)
WHERE (to.`to_prop` in $UnwantedValues)
WITH COLLECT(to) AS unwantedEndpoints
// only use the 'from' nodes that do not have a relation to one of the unwantedEndpoints
MATCH p=(from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE NONE( n IN [(from)-[:SPECIAL_REL]->(m) | m] WHERE n IN unwantedEndpoints)
RETURN p
==== 已更新,并应用于电影数据集====
// be careful not to re-use the bindings!
MATCH (persons:Person)-[a:ACTED_IN]->(m:Movie)
WHERE ((m.title in ["Speed Racer"]))
with collect(m) as excluded
MATCH (p:Person)-[:ACTED_IN]->(:Movie)
WHERE none(n in [(p)-[:ACTED_IN]->(m2:Movie)| m2] where n in excluded)
RETURN DISTINCT p.name,[(p)-[:ACTED_IN]->(m3:Movie)| m3.title]
ORDER BY p.name
假设:
- 我的 Neo4j 数据库中有
SpecialFromLabel
个节点。 - 这些节点连接到
SpecialToLabel
个具有SPECIAL_REL
关系的节点 - 因此,存在以下路径:
p = (from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
- 具有
SpecialToLabel
的节点有一个 属性to_prop
可以有不同的字符串值
我如何构造一个查询,为我提供具有路径的所有 from
个节点:p=(from: SpecialFromLabel)-[:SPECIAL_REL]->(to: SpecialToLabel)
- 但 那些 from
个节点没有通往 to
个节点的路径,其中 to.to_prop
在不需要的值集中?
在伪代码中(不可运行)我想要这样的东西:
MATCH (unwanted_from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE (to.`to_prop` in $UnwantedValues)
with unwanted_from
MATCH (from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE from not in unwanted_from
RETURN from
我会这样处理
// collect the unwanted endpoints
MATCH (to: SpecialToLabel)
WHERE (to.`to_prop` in $UnwantedValues)
WITH COLLECT(to) AS unwantedEndpoints
// only use the 'from' nodes that do not have a relation to one of the unwantedEndpoints
MATCH p=(from: SpecialFromLabel)-[r:SPECIAL_REL]->(to: SpecialToLabel)
WHERE NONE( n IN [(from)-[:SPECIAL_REL]->(m) | m] WHERE n IN unwantedEndpoints)
RETURN p
==== 已更新,并应用于电影数据集====
// be careful not to re-use the bindings!
MATCH (persons:Person)-[a:ACTED_IN]->(m:Movie)
WHERE ((m.title in ["Speed Racer"]))
with collect(m) as excluded
MATCH (p:Person)-[:ACTED_IN]->(:Movie)
WHERE none(n in [(p)-[:ACTED_IN]->(m2:Movie)| m2] where n in excluded)
RETURN DISTINCT p.name,[(p)-[:ACTED_IN]->(m3:Movie)| m3.title]
ORDER BY p.name