0
votes

I am relatively new to Cypher and neo4j and and was playing with a simple scenario with 2 bus stops 1) BusStop1 1- [:Next]- BusStop 2 (i.e 2 Bus Stop node with a relation Next between them.

2) Number 35 node is related to BusStop1 and BusStop2 with a Relationship (STOPS_AT) . So if i query minimum path between BusStop1 and BusStop2 i get the busstops and the bus 35 that can be used to travel.

3) Number35 node is also related to Company XYz with a relationship OPERATED_BY, so i know the company XYZ is the one running Bus Number 35.

My Problem begins with once i get the node list in EXTRACT i have a WHEN case for the busstops and Number 35, but then how do i travel to Company XYZ inside the WHEN case

MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(d)) (---works fine---)

RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Bus THEN 'Bus ' + x.id

(((----At this point when the node is a bus number i need to also traverse the relationship to its Operator ?????? how ))) ,

WHEN x:BusStop THEN 'BusStop ' + x.name

1

1 Answers

1
votes

This query will return the operating companies for the distinct list of buses that stop at bus stops.

You will want to contain this somehow (maybe by stop or collection of stops) otherwise it will literally match every occurrence of a bus stopping at a stop. Not a problem in your small data set but in a bigger data set it could return a lot of data and would just result in a collection of all of the buses and all of the companies for which their would be a far cheaper alternate query.

// simplified version of your first line with some labels
match p=(a:Bus)-[r:STOPS_AT]->(d:Stop)
// filter the bus nodes and extract them fro the result
with [b IN nodes(p) WHERE labels(b)[0] = 'Bus'] as buses
// remove the duplicate buses from the result set
unwind buses as b 
with collect(distinct b) as buses
// iterate over the distinct buses and find the operating company
unwind buses as b
match b-[:OPERATED_BY]->(c:Company)
return b.name as Bus, c.name as Company

Are you just after stops, buses and operators? If so, another way to go might be just to ask for that as so...

match (c:Company)<-[:OPERATED_BY]-(a:Bus)-[r:STOPS_AT]->(d:Stop)
return c.name, a.name, d.name