Cypher确定共享节点是否存在路径

问题描述 投票:0回答:1

我有一个如下图所示的示例图。

enter image description here节点具有以下标签:

  • 节点(a)有标签User
  • 节点(b)直到(g)有标签Attribute
  • 节点(h)和(i)具有标签Object

节点(a)与(d)和(e)有关系

我想检查哪个Object节点(h)和(i)与用户所连接的所有Attribute节点有关系,即(d)和(e)。

所以

  • 节点(h)与(d)AND到(e)有关系 - >这没关系
  • 节点(i)只与(e)有关系 - >这不行

我如何在Cypher查询中检测到这一点?

我从以下开始:

MATCH p1 = (u:User)-->(ua:Attribute)-->(oa:Attribute)
WITH *
MATCH p2 = (oa)<-[*]-(o:Object)
RETURN p1,p2

这给了我完整的图表。但是我如何改进这个以获得(h)而不是(i)。

我想我必须从第一部分建立一个结果列表,并验证所有这些是否存在关系?

neo4j cypher
1个回答
2
投票

您可以使用ALL谓词来检查:

// First retrieve the Attribute nodes the User is connected to and make it a collection
MATCH (u:User)-[:ASSOCIATED_TO]->(attribute)
WITH u, collect(attribute) AS attributes
// Find the objects having relationships to ALL elements in attributes
MATCH (o:Object) WHERE 
ALL( x IN attributes WHERE (o)--(x) )
RETURN u, attributes, collect(o) AS objects
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.