在Neo4J中,我如何匹配子图,但过滤另一个子图中的节点

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

我的图是一个有向的非周期图。我想查询给定父节点的子图,但过滤掉与另一个父节点有关系的子图。我创建了一个图像来说明。

例如,我想选择子图(C)-[r]->(D),但排除(B)-[r]->(E),因为它是(A)的孩子。

我写了以下Cypher查询,其中包括对apoc.path.subpgrahAll()的调用,它过滤掉了(B),但它没有过滤掉像(E)这样的节点

MATCH (n {id: 'C'})
CALL apoc.path.subgraphAll(n, {relationshipFilter: 'CONNECTED>'})
YIELD nodes, relationships
UNWIND nodes as node
WITH node
WHERE SIZE(()-[:CONNECTED]->(node)) = 1
RETURN node

enter image description here

neo4j cypher spring-data-neo4j neo4j-apoc
1个回答
0
投票

这可能对你有用:

CALL apoc.path.subgraphAll(n, {relationshipFilter: 'CONNECTED>'}) YIELD nodes, relationships
UNWIND nodes as node
WITH node
WHERE SIZE((node)<-[:CONNECTED]-()) = 1
RETURN node

顺便说一句,在Cypher模式中,您需要在关系类型之前加上冒号,这是您查询的问题之一(但不是主要问题)。

© www.soinside.com 2019 - 2024. All rights reserved.