在 Cypher 中多次遍历关系
我有一张机场图、机场之间的路线以及承运该机场的航空公司的图表。我将路线创建为单独的节点,而不仅仅是一种关系,以便我可以将每个路线与航空公司和其他节点连接起来。
每个Route
节点与始发机场有一个IS_FROM
关系,与目的地机场有一个IS_TO
关系。它还与其航空公司有 IS_BY
关系:
我正在尝试遍历这棵树n
次,以获取两个机场之间的路线。例如,如果n = 3
,我想获取从LAX
到LHR
的所有路线,有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:
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 Airline
s that connect to those routes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以首先提取距离 3 跳或更少的机场之间的所有路径,然后使用 OPTIONAL MATCH 来查看路径中的哪些节点是路线,以及哪些航空公司提供这些路线。
注意:可变长度路径不允许传递参数,因此您不能执行类似
[*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.Caveat: Variable-length paths don't allow passing parameters, so you can't do something like
[*2..2*n]
.我不知道你的问题我是否答对了。对我来说你的问题可以这样解决:
I don't know if i got your question right. To me your problem could be solved this way: