title: Getting indirect routes in neo4j considering the order in which they appear in a route
body: <p>So I am trying to get indirect routes in my neo4j graph consisting of stop and route nodes. </p>
  
  <p>Here's my ER diagram of my relational database. </p>
  
  <p><a href="https://i.stack.imgur.com/t61xz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t61xz.png" alt="enter image description here"></a></p>
  
  <p>My model consists of stop nodes with node having name of each stop (The blue coloured nodes in the picture).</p>
  
  <p>I have multiple routes which have the relationship(STOPS_AT) with the stop nodes. These relationship points to the various stops that a bus will make when it's following a particular route.</p>
  
  <p>The Stops at relationship has the following properties. </p>
  
  <p>fare_bt_stops - It has the fare of trip between this stop and the previous stop.
  order - Order in which bus stops at stop. Origin being 0</p>
  
  <p>This is how my STOPS_AT relation looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/Lia43.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lia43.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/p6qVj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p6qVj.png" alt="enter image description here"></a></p>
  
  <p>So to get the shortest distance between A to C I'm running this query to get the route between A and C.</p>
  
  <pre><code>MATCH (a:Stop {name:'A'}), (d:Stop {name:'C'})
  MATCH p = allShortestPaths((a)-[:STOPS_AT*..12]-(d))
  RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Stop THEN 'Stop ' + x.name
                                      WHEN x:Route THEN 'Route ' + x.name
                                      ELSE '' END) AS itinerary
  </code></pre>
  
  <p>Which gives output <code>[Stop 12, Route 4, Stop 13, Route 5, Stop 14]</code></p>
  
  <p>But how do I query this model to get the path with the least fare. I have seen other examples for it but in those examples the nodes were directly connected whereas in mine they are connected via Route nodes. </p>
  
  <p>Plus how do I validate the routes according to the stop_order property for example Route <code>A to B</code> stops at A first and then on B while Route <code>C to B</code> stops at C first and then B so there should not be any way to reach from A to C but this query is not handling this particular case as well. So right now I am handling that in my code which I think is not the best way to do it.</p>
  
  <p>I am stuck on this for the past two days so would appreciate any help that I can get.</p>