Neo4J/Cypher: How to filter the nodes of a path?
<p>I want to get all the <code>simple cycles</code>/<code>circuits</code> on a graph that go through a given node. I am able to  so with this cypher query:</p>

<pre><code>MATCH p=(n)-[*]-&gt;(n)
WHERE n.postId = 71 //postId is a node property
RETURN nodes(p)
</code></pre>

<p>However, the above retrieves duplicate nodes within the 'circuit' (except from the start and the end nodes), which is not a circuit at all according to the graph theory.</p>

<p>With the following query I can remove those duplicates in the circuit but I have to limit the length of the circuit or path in the <code>MATCH</code> pattern which is kind of hardcoding it.</p>

<pre><code>// In this example the length of the path is hardcoded to 4
MATCH p=
    (n)-[:RELATES_TO]-&gt;
    (p2)-[:RELATES_TO]-&gt;
    (p3)-[:RELATES_TO]-&gt;
    (p4)-[:RELATES_TO]-&gt;(n)
WHERE n.postId = 71
    AND p2.postId &lt;&gt; 71
    AND p3.postId &lt;&gt; 71
    AND p4.postId &lt;&gt; 71
RETURN nodes(p)
</code></pre>

<p>Is there a way to filter the nodes between the relationships in the first query?</p>

<pre><code>MATCH p=(n)-[*]-&gt;(n)
WHERE n.postId = 71 //postId is a node property
RETURN nodes(p)
</code></pre>

<p>Important notes:</p>

<ul>
<li>I know how to limit the length of the path (via the WHERE length() constraint or with <a href="https://neo4j.com/docs/developer-manual/current/cypher/#_variable_length" rel="nofollow">variable length relationships</a>)</li>
</ul>