title: Matching nodes with variable and set number of relationship of many types
body: <p>I have a tree of nodes that is connected with the relationship types: <code>PARENT_OF</code> and <code>NEXT</code>, and need to match all nodes <code>n</code> starting from the root to a variable depth <code>num</code>. Nodes <code>n</code> need to be direct children that are reached through <code>PARENT_OF</code>. </p>
  
  <p>(the depth increments only through relationships of type <code>PARENT_OF</code>)</p>
  
  <p>I have writte the following query:</p>
  
  <pre><code>MATCH p=(:HeadArgument)-[:PARENT_OF|:NEXT*]-&gt;(node) 
  WHERE ()-[:PARENT_OF]-&gt;(node) AND size(filter(x IN relationships(p) WHERE type(x) = 'PARENT_OF')) &lt;= {num} 
  RETURN node
  </code></pre>
  
  <p>But it's inefficient because it matches on paths of all depths first and then filters out all paths that are too long (which can be extremely long and too many). I need a query that only reaches to set depths like the following examples with wrong syntax: <code>()-[(:NEXT*|:PARENT_OF)*..{num}]-()</code> (adding up variable paths like regular expressions): <code>()-{([:NEXT*]-()-[:PARENT_OF])*..{num}}-()</code> or another way if you know of any other ways.</p>
  
  <p>Thanks.</p>