[英]How to get a path from one node to another including all other nodes and relationships involved in between
I have designed a model in Neo4j in order to get paths from one station to another including platforms/legs involved. 我在Neo4j中设计了一个模型,以便获得从一个工作站到另一个工作站(包括所涉及的平台/支腿)的路径。 The model is depicted down here. 该模型如下所示。 Basically, I need a query to take me from NBW to RD. 基本上,我需要一个查询才能将我从NBW带到RD。 also shows the platforms and legs involved. 还显示了涉及的平台和腿。 I am struggling with the query. 我正在努力查询。 I get no result. 我没有结果。 Appreciate if someone helps. 感谢有人帮助。
Here is my cypher statement: 这是我的密码声明:
MATCH p = (a:Station)-[r:Goto|can_board|can_alight|has_platfrom*0..]->(c:Station)
WHERE (a.name='NBW')
AND c.name='RD'
RETURN p
As mentioned in the comments, in Cypher you can't use a directed variable-length relationship that uses differing directions for some of the relationships. 如评论中所述,在Cypher中,您不能使用针对某些关系使用不同方向的有向可变长度关系。
However, APOC Procedures just added the ability to expand based on sequences of relationships. 但是, APOC程序刚刚添加了根据关系序列进行扩展的功能。 You can give this a try: 您可以尝试一下:
MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='THT'
CALL apoc.path.expandConfig(start, {terminatorNodes:[end], limit:1,
relationshipFilter:'has_platform>, can_board>, goto>, can_alight>, <has_platform'}) YIELD path
RETURN path
I added a limit so that only the first (and shortest) path to your end station will be returned. 我添加了一个限制,以便仅返回到终端站的第一个(也是最短的)路径。 Removing the limit isn't advisable, since this will continue to repeat the relationships in the expansion, going from station to station, until it finds all possible ways to get to your end station, which could hang your query. 不建议取消限制,因为这将继续在扩展中重复从站到站的关系,直到找到找到到达终端站的所有可能方式,这可能会挂起查询。
EDIT 编辑
Regarding the new model changes, the reason the above will not work is because relationship sequences can't contain a variable-length sequence within them. 关于新模型的更改,上述方法不起作用的原因是关系序列中不能包含可变长度序列。 You have 2 goto>
relationships to traverse, but only one is specified in the sequence. 您有2个goto>
关系可以遍历,但是序列中仅指定了一个。
Here's an alternative that doesn't use sequences, just a whitelisting of allowed relationships. 这是一种不使用序列的替代方法,只是将允许的关系列入白名单。 The spanningTree()
procedure uses NODE_GLOBAL uniqueness so there will only be a single unique path to each node found (paths will not backtrack or revisit previously-visited nodes). spanningTree()
过程使用NODE_GLOBAL唯一性,因此到找到的每个节点将只有一条唯一路径(路径不会回溯或重新访问以前访问的节点)。
MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='RD'
CALL apoc.path.spanningTree(start, {terminatorNodes:[end], limit:1,
relationshipFilter:'has_platform>|can_board>|goto>|can_alight>|<has_platform'}) YIELD path
RETURN path
Your query is directed -->
and not all of the relationships between your two stations run in the same direction. 您的查询是针对-->
,并非两个工作站之间的所有关系都朝相同的方向运行。 If you remove the relationship direction you will get a result. 如果删除关系方向,您将得到结果。
Then once you have a result I think something like this could get you pointed in the right direction on extracting the particular details from the resulting path once you get that working. 然后,一旦获得结果,我认为类似的事情可能会使您指出正确的方向,以便在工作后从结果路径中提取特定细节。
Essentially I am assuming that everything you are interested in is in your path that is returned you just need to filter out the different pieces that are returned. 本质上,我假设您感兴趣的一切都在返回的路径中,您只需要过滤掉返回的不同部分即可。
As @InverseFalcon points out this query should be limited in a larger graph or it could easily run away. 正如@InverseFalcon指出的那样,此查询应限制在较大的图中,否则可能会轻易消失。
MATCH p = (a:Station)-[r:Goto|can_board|can_alight|has_platfrom*0..]-(c:Station)
WHERE (a.name='NBW')
AND c.name='THT'
RETURN filter( n in nodes(p) WHERE 'Platform' in labels(n)) AS Platforms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.