如何从neo4j中的2个不同关系中获取输出

How to get output from 2 different relation in neo4j

我的模型是运输系统:

节点:BusStop、Bus、TransportOperator

关系:BusStop-[:Stops_At]->Bus

关系:总线-[Operated_By]->TransportOperator

如果我使用下面的查询,我得到一个输出:

查询:

MATCH (a:BusStop{name:'Bonhoefferstrasse'}),(d:BusStop {name:'HeidelBerg Hauptbanhof'})
MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(d))

RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Bus THEN 'Bus' + x.id   WHEN x:BusStop THEN 'BusStop'+ x.name
ELSE '' END) AS RouteDetails

输出:

BusStopBonhoefferstrasse, Bus34, BusStopHeidelBerg Hauptbanhof

但是从上面的关系来看,如果我想让运算符显示在输出中,我该如何查询...? neo4j 是否提供了执行此操作的功能?

例如 :(这个查询是错误的,只是为了给出我想要得到的输出)

MATCH (a:BusStop{name:'Bonhoefferstrasse'}),(d:BusStop {name:'HeidelBerg Hauptbanhof'}),(e:Bus{id:''}),(f:TransportOperator{name:'Rhein-Neckar-Verkehr'})
MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(d)),((e)-[:OPERATED_BY]->(f))**

RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Bus THEN 'Bus' + x.id   WHEN x:BusStop THEN 'BusStop'+ x.name WHEN x:TransportOperator THEN 'TransportOperator' ELSE '' END) AS RouteDetails

我可以与 club 2 建立关系吗(在上面的匹配查询中使用)?

预期 output:BusStopBonhoefferstrasse、Bus34、RNV、BusStopHeidelBerg Hauptbanhof

this answer 的扩展:

示例数据:

CREATE (a:Stop {name:'A'}),
       (b:Stop {name:'B'}),
       (c:Stop {name:'C'}),
       (d:Stop {name:'D'}),

       (a)-[:NEXT {distance:1}]->(b),
       (b)-[:NEXT {distance:2}]->(c),
       (c)-[:NEXT {distance:3}]->(d),

       (b1:Bus {id:1}),
       (b2:Bus {id:2}),
       (b3:Bus {id:3}),

       (o1:Operator {id:1}),
       (o2:Operator {id:2}),

       (b1)-[:OPERATED_BY]->(o1),
       (b2)-[:OPERATED_BY]->(o1),
       (b3)-[:OPERATED_BY]->(o2),

       (b1)-[:STOPS_AT]->(a),
       (b1)-[:STOPS_AT]->(b),
       (b2)-[:STOPS_AT]->(a),
       (b2)-[:STOPS_AT]->(b),
       (b2)-[:STOPS_AT]->(c),
       (b3)-[:STOPS_AT]->(b),
       (b3)-[:STOPS_AT]->(c),
       (b3)-[:STOPS_AT]->(d);

解决方案:

MATCH (a:Stop {name:'A'}), (d:Stop {name:'D'})
MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(d))
WITH p, FILTER(x IN NODES(p) WHERE x:Bus) AS buses
UNWIND buses AS bus
MATCH (bus)-[:OPERATED_BY]->(o:Operator)
RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Stop THEN 'Stop ' + x.name
                                    WHEN x:Bus THEN 'Bus ' + x.id
                               ELSE '' END) AS itinerary,
       COLLECT('Bus ' + bus.id + ':' + 'Operator ' + o.id) AS operators

结果:

itinerary                               operators
[Stop A, Bus 2, Stop B, Bus 3, Stop D]  [Bus 2:Operator 1, Bus 3:Operator 2]
[Stop A, Bus 1, Stop B, Bus 3, Stop D]  [Bus 1:Operator 1, Bus 3:Operator 2]
[Stop A, Bus 2, Stop C, Bus 3, Stop D]  [Bus 2:Operator 1, Bus 3:Operator 2]

控制台:http://console.neo4j.org/r/p2xgiy