简体   繁体   English

如何从Neo4j的Node M开始获得第N级方向的完整子图

[英]How to get complete subgraph with direction at Nth level starting at Node M in Neo4j

I asked this question How to get subgraph a while back and found while it does give me all nodes and relationships at the Nth level, the answers assume the direction of the relationships either fan out from the starting node or converge to the node. 我问了这个问题, 如何获得子图 ,并确实找到了第N个层的所有节点和关系,答案假定关系的方向从起始节点散开或收敛到该节点。 What I am looking for is a complete subgraph that captures all relationships and nodes at a certain level while preserving and relating the direction of the relationships. 我要寻找的是一个完整的子图,该子图可以在保留和关联关系方向的同时捕获特定级别的所有关系和节点。

For example, if I want all nodes and relationships at depth 2 I can make a directionless query as follows: 例如,如果我希望深度为2的所有节点和关系,可以进行如下无方向查询:

START n=node(12345) MATCH (n)<-[r*1..2]->(m) RETURN r, m;

This works well as I get all nodes and all relationships, but I find I have no idea what direction the relationships are in. 当我获得所有节点和所有关系时,此方法效果很好,但是我不知道关系的方向。

If I limit the search to a depth of 1 and perform two searches, one incoming and one outgoing, I can get all nodes and relationships with direction that way. 如果我将搜索限制为1的深度,并执行两次搜索(一次传入和一次传出),则可以以这种方式获得所有节点和关系。 I can then recursively do the same query for all nodes found at depth two, discarding any relationships that contain nodes not found beyond the 2nd level from the start. 然后,我可以递归地对在深度2处找到的所有节点执行相同的查询,从一开始就丢弃包含未在第二级之外找到的节点的任何关系。 This seems rather painful and manual but it works. 这似乎很痛苦并且很手动,但是可以工作。

I've also tried using the TraversalDescription framework in the embedded Java API, but that doesn't quite return all relationships. 我也尝试过在嵌入式Java API中使用TraversalDescription框架,但这并不能完全返回所有关系。 For example, the following snippet gave me all nodes at depth 2, but it missed some relationships between nodes at depth 2: 例如,以下代码段为我提供了深度2的所有节点,但是错过了深度2的节点之间的一些关系:

for ( Path position : graphDb.traversalDescription() .breadthFirst() .evaluator( Evaluators.toDepth( 2 ) ) .traverse( templateNode ) ) { output += position + "\\n"; }

Is there an easy way to do this without multiple manual iterations? 有没有一种简单的方法就可以进行多次手动迭代呢?

Directionless queries are without arrow tips. 无方向查询没有箭头提示。

If you want to have a direction use: 如果您想获得指示,请使用:

START n=node(12345)
 MATCH (n)-[r*1..2]->(m)
 RETURN r, m;

You can access start and endnode of the rel and then output them to infer their direction. 您可以访问rel的开始节点和结束节点,然后输出它们以推断其方向。

START n=node(12345)
 MATCH (n)-[rels*1..2]->(m)
 RETURN m, extract(r in rels | [startNode(r),endNode(r),type(r)]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM