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

时间:2015-01-29 22:24:33

标签: neo4j spring-data-neo4j

模型是运输系统:

节点:BusStop,Bus,TransportOperator

关系:Bus- [Operated_By] - > TransportOperator

关系:BusStop - [:Stops_At] - >总线

关系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内卡-Verkehr

预期产出:

行程:BusStopBonhoefferstrasse 部门时间:9:01 ,Bus34,RNV,BusStopHeidelBerg Hauptbanhof ArrTime:9:15

算: Bus34:TransportOperatorRhein-Neckar-Verkehr

3 个答案:

答案 0 :(得分:4)

我们再来一次:

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);

您应该将出发和到达时间作为自己的列返回:

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

答案 1 :(得分:2)

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

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

...
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
...

答案 2 :(得分:1)

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

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

看起来问题出在你的return语句中。您明确要求只返回字符串' BusStop'与公交车站名称连接。

您可以更改提取语句以返回到达和离开时间,但仅在第一个节点上获得到达时间,并且仅在最后一个节点上的出发时间可能需要重新考虑您的查询。这就是您需要在整个过程中为结果添加到达时间所需的内容。根据您的预期输出,这并不是您所需要的,但应该让您知道为什么没有时间输出:

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