我的模型是transoport系统:
节点:BusStop,Bus,TransportOperator
关系:BusStop - [:Stops_At] - >总线
关系:Bus- [Operated_By] - > TransportOperator
如果我使用以下查询,我会得到一个输出:
QUERY:
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
我可以加入2关系(用于上面的匹配查询)吗?
预期产出:BusStopBonhoefferstrasse,Bus34, RNV ,BusStopHeidelBerg Hauptbanhof
答案 0 :(得分:2)
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]