[英]Neo4j cypher - search for nodes with no path between them
I'm trying to find a generic way to search for a node or set of nodes which does not have a link to a another node or set of nodes.我正在尝试找到一种通用方法来搜索没有链接到另一个节点或节点集的节点或节点集。 As an example, I was able to find all the nodes of a specific type (eg
:Style
) which are connected somehow to a specific set of nodes (eg :MetadataRoot
), with the following:例如,我能够找到特定类型(例如
:Style
)的所有节点,这些节点以某种方式连接到一组特定的节点(例如:MetadataRoot
),如下所示:
match (root:MetadataRoot),
(n:Style),
p=shortestPath((root)-[*]-(n))
return p
Using this, I was able to subtract the set of all :Style
nodes from the nodes returned by the above query, but that doesn't seem like the best way to go about this.使用它,我能够从上述查询返回的节点中减去所有
:Style
节点的集合,但这似乎不是 go 的最佳方式。
If you know the label of the start nodes you can use the EXISTS function : 如果您知道起始节点的标签,则可以使用EXISTS功能:
MATCH (n:Style)
WHERE NOT EXISTS((n)-[]-())
RETURN n
If you know the end node : 如果您知道结束节点:
MATCH (n:Style)
WHERE NOT EXISTS ((n)-[*]-(:MetadataRoot))
RETURN n
EDIT : 编辑:
Not sure, but regarding the performance issues in your comment, a workaround could be something like this : 不确定,但是对于评论中的性能问题,解决方法可能是这样的:
MATCH p=allShortestPaths((n:Style)-[*]-(:MetadataRoot))
WITH nodes(p) as nodesRelated
MATCH (s:Style) WHERE NOT s IN nodesRelated
This should be way faster and it should need less resources to execute:这应该更快并且需要更少的资源来执行:
MATCH (n:Style),
OPTIONAL MATCH p=shortestPath((:MetadataRoot)-[*0..40]-(n))
WITH n, p
WHERE p IS NULL
RETURN n ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.