在 Cypher 中多次遍历关系

发布于 2025-01-10 07:36:41 字数 1078 浏览 0 评论 0原文

我有一张机场图、机场之间的路线以及承运该机场的航空公司的图表。我将路线创建为单独的节点,而不仅仅是一种关系,以便我可以将每个路线与航空公司和其他节点连接起来。

每个Route 节点与始发机场有一个IS_FROM 关系,与目的地机场有一个IS_TO 关系。它还与其航空公司有 IS_BY 关系: 输入图片这里的描述

我正在尝试遍历这棵树n次,以获取两个机场之间的路线。例如,如果n = 3,我想获取从LAXLHR的所有路线,有3个或更少的连接。

基本上,我的结果将是以下各项的并集: 无中转机场:

MATCH (a1:Airport {iata : 'LAX'})<-[:IS_FROM]-(r:Route)-[:IS_TO]->(a2:Airport {iata : 'LHR'}), (r)-[:IS_BY]->(ai:Airline) return a1 , r , a2 , ai;

1 中转机场:

MATCH (a1:Airport {iata : 'LAX'})<-[:IS_FROM]-(r:Route)-[:IS_TO]->(a2:Airport)<-[IS_FROM]-(r2:Route)-[:IS_TO]->(a3:Airport {iata: 'LHR'}), (r2)-[:IS_BY]->(ai:Airline) return a1 , r , a2 , a3 , r2 , ai;

等等。

因此查询应该动态遍历 (:Airport)<-[:IS_FROM]-(:Route)-[:IS_TO]->(:Airport) 模式 n 次,并返回节点(我更感兴趣的是返回连接到这些路线的航空公司。

I have a graph of Airports, Routes between them and Airlines that carry it. I created routes as separate nodes, rather than just a relationship, so that I can connect each with an Airline, and other nodes.

Each Route node has an IS_FROM relationship with the origin airport and an IS_TO relationship with the destination. It also has an IS_BY relationship with its airline:
enter image description here

I am trying to traverse this tree, n times, for routes between two airports. For example, if n = 3, I want to get all the routes, that will lead from LAX to LHR, with 3 or fewer connections.

So basically, my result would be a union of the following:
No Connecting Airports:

MATCH (a1:Airport {iata : 'LAX'})<-[:IS_FROM]-(r:Route)-[:IS_TO]->(a2:Airport {iata : 'LHR'}), (r)-[:IS_BY]->(ai:Airline) return a1 , r , a2 , ai;

1 Connecting airports:

MATCH (a1:Airport {iata : 'LAX'})<-[:IS_FROM]-(r:Route)-[:IS_TO]->(a2:Airport)<-[IS_FROM]-(r2:Route)-[:IS_TO]->(a3:Airport {iata: 'LHR'}), (r2)-[:IS_BY]->(ai:Airline) return a1 , r , a2 , a3 , r2 , ai;

and so on.

So the query should dynamically traverse the (:Airport)<-[:IS_FROM]-(:Route)-[:IS_TO]->(:Airport) pattern n times, and return the nodes (I am more interested in returning the Airlines that connect to those routes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2025-01-17 07:36:41

您可以首先提取距离 3 跳或更少的机场之间的所有路径,然后使用 OPTIONAL MATCH 来查看路径中的哪些节点是路线,以及哪些航空公司提供这些路线。

MATCH path = (:Airport {iata:$start_airport})-[*2..6]-(:Airport {iata:$end_airport})
WITH path,nodes(path) as path_airports_and_connecting_routes
UNWIND path_airports_and_connecting_routes as node
OPTIONAL MATCH (node)-[:IS_BY]-(airline:Airline)
WITH collect(node) as airports_and_routes,airline
RETURN airports_and_routes + [airline]

注意:可变长度路径不允许传递参数,因此您不能执行类似 [*2..2*n] 的操作。

You can first extract all the paths between Airports that are 3 or less hops away, and then use OPTIONAL MATCH to see which of the nodes in the path are Routes, and which Airlines are offering them.

MATCH path = (:Airport {iata:$start_airport})-[*2..6]-(:Airport {iata:$end_airport})
WITH path,nodes(path) as path_airports_and_connecting_routes
UNWIND path_airports_and_connecting_routes as node
OPTIONAL MATCH (node)-[:IS_BY]-(airline:Airline)
WITH collect(node) as airports_and_routes,airline
RETURN airports_and_routes + [airline]

Caveat: Variable-length paths don't allow passing parameters, so you can't do something like [*2..2*n].

煮酒 2025-01-17 07:36:41

我不知道你的问题我是否答对了。对我来说你的问题可以这样解决:

MATCH (a1:Airport {iata : 'LAX'})<-[r1:IS_FROM]-(r:Route)-[r2:IS_TO]->(a2:Airport{iata : 'LHR'})
OPTIONAL MATCH (r)-[r3:IS_BY]->(ai:Airline)
RETURN a1,r1,r,r2,a2,r3,ai

I don't know if i got your question right. To me your problem could be solved this way:

MATCH (a1:Airport {iata : 'LAX'})<-[r1:IS_FROM]-(r:Route)-[r2:IS_TO]->(a2:Airport{iata : 'LHR'})
OPTIONAL MATCH (r)-[r3:IS_BY]->(ai:Airline)
RETURN a1,r1,r,r2,a2,r3,ai
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文