Reputation: 1581
I am fairly new to Neo4j and I'm trying to build a small example app: a public transport planner. It looks as follows (obviously strongly simplified example):
The departure and arrival time of each bus (trip) is stored as a relationship property. I want the bus to leave near (or after) a specified time, while arriving as soon as possible after the departure (i.e. not necessarily the shortest, but the fastest route).
I have experimented a lot in Cypher, but I have not been able to do this. Any advice?
EDIT: updated diagram
Upvotes: 2
Views: 146
Reputation: 67044
To enable a simpler Cypher query, you can change your graph model to something like:
(:Trip)-[:LEG {start: 10, end: 20}]->(:Stop)-[:LEG {start: 30, end: 40}]->(:Stop) ...
| |
[:AT] [:AT]
| |
v v
(Location {name: "Paris"}) (Location {name: "London"})
To summarize, each Trip consists of a chain of N Legs and N Stops. Each Stop is related to a Location, and each Leg stores its start and end times.
With the above model, the following query will return: (a) the path of the fastest Trip that started at or after time 40, and (b) the duration of that Trip.
MATCH p=(t:Trip)-[legs:LEG*1..]->(s:Stop)-[:AT]->(loc:Location)
WHERE HEAD(legs).start >= 40 AND NOT (s)-[:LEG]->()
WITH p, LAST(legs).end - HEAD(legs).start AS duration
RETURN p, duration ORDER BY duration LIMIT 1;
Here is a console that shows sample results.
Upvotes: 2