如何从neo4j中的2个不同关系获取出发时间和到达时间

How to get departure time and arrival time from 2 different relation in neo4j

模型是运输系统:

节点:BusStop、Bus、TransportOperator

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

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

关系 STOPS_AT 有 2 个属性到达时间 (9:00) 和出发时间 (9:01) 连接到所有公交车站。

示例:34 路公交车连接到 BusStop1(ArrTime-9:00, DeptTime-9:01), BusStop2(ArrTime-9:10, DeptTime-9:11), BusStop3(ArrTime-9:15) , DeptTime-9:16)

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

enter code here

MATCH (a:BusStop{name:'Bonhoefferstrasse'}),(d:BusStop {name:'HeidelBerg Hauptbanhof'})
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:TransportOperator)
RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:BusStop THEN 'BusStop' + x.name
    WHEN x:Bus THEN 'Bus' + x.id
    ELSE '' END) AS itinerary,
COLLECT ('Bus' + Bus.id+ ':' + 'TransportOperator' + o.name) AS Operators

输出:

行程: BusStopBonhoefferstrasse, Bus34, BusStopHeidelBerg Hauptbanhof

运营商: Bus34:TransportOperatorRhein-Neckar-Verkehr

预期输出:

行程:BusStopBonhoefferstrasse 出发时间:9:01,Bus34,RNV,BusStopHeidelBerg Hauptbanhof ArrTime:9:15

运营商: Bus34:TransportOperatorRhein-Neckar-Verkehr

编辑:我误读了数据模型,所以这个答案实际上不起作用。

============================================= =======

看来问题出在您的 return 语句中。您明确表示仅 return 字符串 'BusStop' 与公共汽车站名称连接。

您可以将提取语句更改为 return 到达和离开时间,但仅在第一个节点上获取到达时间并仅在最后一个节点上获取出发时间可能需要重新考虑您的查询。以下是您需要将到达时间全面添加到结果中的内容。根据您的预期输出,这并不是您所需要的,但应该让您明白为什么没有时间被输出:

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

我们再来一次:

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 {arrival:'9:00', departure:'9:01'}]->(a),
       (b1)-[:STOPS_AT {arrival:'9:10', departure:'9:11'}]->(b),
       (b2)-[:STOPS_AT {arrival:'9:05', departure:'9:06'}]->(a),
       (b2)-[:STOPS_AT {arrival:'9:20', departure:'9:21'}]->(b),
       (b2)-[:STOPS_AT {arrival:'9:29', departure:'9:30'}]->(c),
       (b3)-[:STOPS_AT {arrival:'9:45', departure:'9:46'}]->(b),
       (b3)-[:STOPS_AT {arrival:'9:50', departure:'9:51'}]->(c),
       (b3)-[:STOPS_AT {arrival:'9:57', departure:'9:58'}]->(d);

您应该 return 出发和到达时间作为它们自己的列:

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,
       HEAD(RELATIONSHIPS(p)).departure AS departure_time,
       LAST(RELATIONSHIPS(p)).arrival AS arrival_time,
       COLLECT('Bus ' + bus.id + ':' + 'Operator ' + o.id) AS operators

http://console.neo4j.org/r/x8fx3b

您可以遍历关系集合,而不是遍历路径中的节点集合。

我认为像这样的东西会return你想要的结果。

...
EXTRACT(s IN relationships(p) | CASE 
WHEN 'BusStop' in labels(startNode(s)) THEN 'BusStop' + (startNode(s)).name + ' ' + s.DepTime
WHEN 'BusStop' in labels(endNode(s)) THEN 'BusStop' + (endNode(s)).name + ' ' + s.ArrTime
WHEN 'Bus' in labels(startNode(s)) THEN 'Bus' + (startNode(s)).name
ELSE '' END) AS itinerary
...