如何获取所有连接的节点,不包括特定的关系 [英] How to get all connected nodes, excluding specific relationships

查看:128
本文介绍了如何获取所有连接的节点,不包括特定的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种检索所有连接节点的高性能方法. 但是有一个转折.我想排除通过某些关系类型连接的节点和子节点.

I'm looking for a performant way of retrieving all connected nodes. However there is a twist. I would like to exclude nodes and consequent children, that are connected via certain relationship types.

附图说明了我的情况.

有两个或更多节点簇.我想检索单个群集的所有节点,具体取决于查询中的ID. 不包括通过"LINK ..."关系连接的所有其他节点(来自不同群集).

There are two or more clusters of nodes. I would like to retrieve all nodes of a single cluster, depending on the id inside the query. All other nodes (coming from different clusters) and connected via "LINK..." relations shall not be included.

我知道如何通过以下方式检索所有连接的节点:

I know how to retrieve all connected nodes via:

MATCH (n:MyNode {id : 123})-[*]-(connectedNodes) RETURN connectedNodes

MATCH (n:MyNode {id : 123})-[*]-(connectedNodes) RETURN connectedNodes

使用WHERE子句进行过滤听起来像是一个坏主意,因为它仍将获取整个图形. APOC程序中是否有某些东西可以让我以这种方式做某事?非常感谢您的帮助.

Filtering with the WHERE clause sounds like a bad idea, because it would still fetch the whole graph. Is there maybe something inside the APOC procedures, that would allow me to do something in that manner? Thanks a lot already for your help.

沙发手我尝试了注释中给出的第一个建议,但是执行时间不够.毕竟,我将尝试限制关联和节点类型.我也尝试了使用递归函数在Python中自定义实现.尚未完成.

EDIT 1: sofar I tried the first suggestion given in the comments but the execution time was not sufficient. I will try to restrict relationahip and node types afterall. Also I tried a custom implementation inside Python using a recursive function. Not finalized yet though.

@InverseFalcon的建议很奏效.首先过滤一次不考虑的所有可用关系类型,然后对各个起始节点和有效关系类型应用apoc.path.subgraphNodes过程.谢谢你.

EDIT 2: @InverseFalcon's suggestion worked liked a charm. First filter all available relationship types for the once that shall not be considered and then applying the apoc.path.subgraphNodes procedure with the respective starting node and the valid relationship types. Thank you.

推荐答案

Tezra的答案很不错,您需要返回DISTINCT connectedNodes,否则将获得重复项,但是在高度连接的图上,这可能需要一段时间(甚至挂起),具体取决于节点的数量,因为Cypher对所有可能的匹配路径都感兴趣,并且这很快就会失去控制.

Tezra's answer has some good points, and you'll want to return DISTINCT connectedNodes otherwise you'll get duplicates, but on a highly connected graph this may take awhile (or even hang) depending on the number of nodes, since Cypher is interested in all possible paths for matches, and that can quickly get out of control.

对于APOC,我们可以处理这种情况,但是正如Tezra所说,我们没有办法将关系列入黑名单,即使有,我们也没有办法根据关系类型的部分名称将其列入黑名单

For APOC we can handle this case, but as Tezra remarked we don't have a way to blacklist relationships, and even if we had that, we don't have a way to blacklist based on partial names of the relationship types.

您需要使用的方法是先获取所有关系类型,然后删除以LINK开头的任何关系类型,然后将其余关系列表连接到以|分隔的字符串中.然后,您可以将其传递给关系过滤器.

The approach you would need to use is to get all relationship types first then remove any which start with LINK, then join the list of remaining relationships into an | separated string. Then you could pass that to the relationship filter.

CALL db.relationshipTypes() YIELD relationshipType
WHERE NOT relationshipType STARTS WITH 'LINK'
WITH collect(relationshipType) as relTypes
WITH apoc.text.join(relTypes, '|') as relTypesString
MATCH (n:MyNode {id : 123})
CALL apoc.path.subgraphNodes(n, {relationshipFilter:relTypesString}) YIELD node
RETURN node as connectedNode

这篇关于如何获取所有连接的节点,不包括特定的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆