Get complete path from a sequence of nodes from Neo4j Graph database with Cypher
<p>I am storing a sequence of nodes, where a node is related to the previous node in the sequence as well as another node representing an 'object'. So for example I might have this sequence:</p>

<pre><code>(S1)&lt;-[r]-(S2)&lt;-[r]-(S3)&lt;-[r]-(S4)
</code></pre>

<p>And then each of the nodes in the sequence are related to another node for e.g.:</p>

<pre><code>(S1)-[r]-&gt;(O1)
(S2)-[r]-&gt;(O2)
(S3)-[r]-&gt;(O1)
(S4)-[r]-&gt;(O3)
</code></pre>

<p>In this example both S1 and S3 are related to O1.</p>

<p>What I want to achieve is to specify the starting point as 'O1' and be able to get the complete path from S1 to S4 in the example above. </p>

<p>I was able to achieve this by adding a property in S1 called 'start' and another in S4 called 'end', to represent the start and end of a sequence and use this query to get the complete sequence only:</p>

<pre><code>MATCH (O:Obj{name:'O1'}), 
path=(O)&lt;-[:OBJECT]-(first:SEQ)&lt;-[:PREV*]-(last:SEQ) 
WHERE has(first.start) 
AND has(last.end) 
return path
</code></pre>

<p>However, I was wondering if there is a better way to achieve this without the 'start' and 'end' properties. The problem I'm encountering when not using the properties is that the sequence is broken down so instead of one sequence I get:</p>

<pre><code>(S1)&lt;-[r]-(S2)
(S1)&lt;-[r]-(S2)&lt;-[r]-(S3)
(S1)&lt;-[r]-(S2)&lt;-[r]-(S3)&lt;-[r]-(S4)
(S3)&lt;-[r]-(S4)
</code></pre>

<p>Is it possible in any way to get the complete sequence only?</p>