Reputation: 449
I have this graph database which contains two types of nodes (labels in neo4j), the node are station and route nodes. There are also two types of edges (relationships in neo4j), connection (CONNECTED_WITH) and routeConnection (USES_STATION). A connection is a bidirectional relationship between two station nodes. A routeConnection is a relationship from a route node to a station node. I know that I can use directed relationships between two stations to solve part of my problem but this would only make other things more complicated since the graph should in my case represent the actual physical railway network (just to be clear that this isn't something I can do for my specific project).
Station node:
(the reason for the UUID stationId is due to other micro services)
The property connectionId on a USES_STATION relationship is a reference to what connection to the next station should be used. This is because multiple connections between two stations are possible. The routeConnection of the last station on a route has null as value for connectionId.
This is what the graph database currently contains:
The problem is as follows. I want to be able to have a startStationId (UUID) and an endStationId (UUID) as a parameter and the query should return all possible routes that are possible to get to from the start station to the end station concidering the direction (determined by connectionId on USES_STATION).
I'm new to Neo4j and the Cypher query language. This is what I was able to filter out based on two stations.
MATCH p=(r1:Route)-[rc1:USES_STATION]-(x:Station)-[c:CONNECTED_WITH*]-(y:Station)-[rc2:USES_STATION]-(r2:Route)
WHERE x.stationId = '05cce0f7-1409-4224-926a-db3b4c4a8ce5' AND y.stationId = '11018de0-1943-42b2-929d-a707f751f79c' AND r1=r2
RETURN p
This is the result with some clarification of the two possible routes. The blue route is the one that I want to be returned due to the direction (from station x to station y).
It still gives me nodes I'm not interested in due to the query not being correct but that doesn't matter I think. The main problem is that I don't know how to only get the route nodes for the requested direction as explained before (from station x to station y. It is also possible that there would be multiple routes needed for the requested stations and I want the query to return all possible paths to the destination station, the above example only would have 1 suitable route node.
If my problem requires more explanation please ask and thanks in advance.
Upvotes: 2
Views: 224
Reputation: 5385
I have the impression that the model you use is part of the problem. You have
(:Route)-[:USES_STATION]->(:Station)
while in fact the connection between two (:Station) nodes is part of the route.
Did you try the following model?
(:Station)-[:START]->(:Connection)-[:END]->(:Station)
combined with
(:Connection)-[:IS_PART_OF]->(:Route)
Finding all the (:Route) nodes is then possible along these lines
// get you start and end point
WITH start,end
// find the itineraries
MATCH itinerary=(start)-[:START|END*]->(end)
// find the connection nodes on the itineraries
WITH itinerary, [n IN nodes(itinerary) WHERE n:Connection] AS connections
// find the routes to which the connections belong
UNWIND connections AS connection
MATCH (connection)-[:IS_PART_OF]->(r:Route)
WITH itinerary,COLLECT(DISTINCT r.name) AS routes
RETURN routes
Upvotes: 1