<p dir="auto">using Michael's idea to create nodes uniquely,</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def prepare_add_user(id)
# returns 0 when created and 1 if it was already there
{:query=&gt;
&quot;start n=node:node_auto_index(name={id}) 
 with count(*) as c 
 where c=0 
 create x={name:{id}} 
 return c&quot;, 
 :params =&gt; {:id =&gt; id}} 
end"><pre class="notranslate"><code class="notranslate">def prepare_add_user(id)
# returns 0 when created and 1 if it was already there
{:query=&gt;
"start n=node:node_auto_index(name={id}) 
 with count(*) as c 
 where c=0 
 create x={name:{id}} 
 return c", 
 :params =&gt; {:id =&gt; id}} 
end
</code></pre></div>
<p dir="auto">If you run this fast enough, you can manage to occasionally create two nodes with the same id. This is not supposed to happen, right?</p>
<p dir="auto">A relatively simple query is causing me some major headaches! It's easy enough to rewrite this query to get it to work, but I cannot figure out why it's returning the wrong results in the first place.</p>
<ul dir="auto">
<li>Neo4j version: 3.0.1-enterprise (via Docker)</li>
<li>Ubuntu 14.04</li>
<li>cypher query via browser interface</li>
</ul>
<h1 dir="auto">Steps to reproduce</h1>
<p dir="auto">Unfortunately, this doesn't actually reproduce the issue :( I have no clue why not</p>
<div class="highlight highlight-source-sql notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="CREATE p=(n:Titled {matchable_title: &quot;left&quot;})-[:of_group]-&gt;(g:Group)&lt;-[:of_group]-(m:Titled {matchable_title: &quot;right&quot;})
RETURN p;

CREATE p=(n:Titled {matchable_title: &quot;same&quot;})-[:of_group]-&gt;(g:Group)&lt;-[:of_group]-(m:Titled {matchable_title: &quot;same&quot;})
RETURN p;

CREATE INDEX ON :Titled(matchable_title);"><pre class="notranslate">CREATE p<span class="pl-k">=</span>(n:Titled {matchable_title: <span class="pl-s"><span class="pl-pds">"</span>left<span class="pl-pds">"</span></span>})<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span><span class="pl-k">&gt;</span>(g:Group)<span class="pl-k">&lt;</span><span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(m:Titled {matchable_title: <span class="pl-s"><span class="pl-pds">"</span>right<span class="pl-pds">"</span></span>})
RETURN p;

CREATE p<span class="pl-k">=</span>(n:Titled {matchable_title: <span class="pl-s"><span class="pl-pds">"</span>same<span class="pl-pds">"</span></span>})<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span><span class="pl-k">&gt;</span>(g:Group)<span class="pl-k">&lt;</span><span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(m:Titled {matchable_title: <span class="pl-s"><span class="pl-pds">"</span>same<span class="pl-pds">"</span></span>})
RETURN p;

<span class="pl-k">CREATE</span> <span class="pl-k">INDEX</span> <span class="pl-en">ON</span> :Titled(matchable_title);</pre></div>
<div class="highlight highlight-source-sql notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="MATCH p=(n:Titled)-[:of_group]-(g)-[:of_group]-(m:Titled)
WHERE m.matchable_title = n.matchable_title
RETURN m.matchable_title, n.matchable_title, p LIMIT 25;"><pre class="notranslate">MATCH p<span class="pl-k">=</span>(n:Titled)<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(g)<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(m:Titled)
<span class="pl-k">WHERE</span> <span class="pl-c1">m</span>.<span class="pl-c1">matchable_title</span> <span class="pl-k">=</span> <span class="pl-c1">n</span>.<span class="pl-c1">matchable_title</span>
RETURN <span class="pl-c1">m</span>.<span class="pl-c1">matchable_title</span>, <span class="pl-c1">n</span>.<span class="pl-c1">matchable_title</span>, p <span class="pl-k">LIMIT</span> <span class="pl-c1">25</span>;</pre></div>
<h1 dir="auto">Expected behavior</h1>
<p dir="auto">For each row of results, m.matchable_title will be the same as n.matchable_title</p>
<h1 dir="auto">Actual behavior</h1>
<p dir="auto">n.matchable_title and m.matchable_title do not match. According to PROFILE, the query starts with a NodeIndexScan to find <code class="notranslate">m</code>. There are multiple different values for m.matchable_title across different rows (as expected). Some of the returned nodes should be in the result set (although the row is wrong, n/m should appear in rows in the correct results) but some m/n nodes would not appear in the correct results.</p>
<p dir="auto">Interestingly, although the query plan is not the same, the rows are returned in the same order as when the WHERE clause is removed.</p>
<p dir="auto">It seems like n is found by an index scan, but the index values are not being checked, or something like that. If I remove the index on matchable_title, then the query starts with a NodeByLabelScan and then proceeds correctly. Adding the index back again results in incorrect rows. If I try to specify to use the matchable_title index, neo4j complains that this is not allowed.</p>
<p dir="auto">The query can be corrected in many different ways. One example is to add  a WITH clause:</p>
<div class="highlight highlight-source-sql notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="MATCH p=(n:Titled)-[:of_group]-(g)-[:of_group]-(m:Titled)
WITH p, n, m
WHERE m.matchable_title = n.matchable_title
RETURN m.matchable_title, n.matchable_title, p LIMIT 25;"><pre class="notranslate">MATCH p<span class="pl-k">=</span>(n:Titled)<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(g)<span class="pl-k">-</span>[:of_group]<span class="pl-k">-</span>(m:Titled)
WITH p, n, m
<span class="pl-k">WHERE</span> <span class="pl-c1">m</span>.<span class="pl-c1">matchable_title</span> <span class="pl-k">=</span> <span class="pl-c1">n</span>.<span class="pl-c1">matchable_title</span>
RETURN <span class="pl-c1">m</span>.<span class="pl-c1">matchable_title</span>, <span class="pl-c1">n</span>.<span class="pl-c1">matchable_title</span>, p <span class="pl-k">LIMIT</span> <span class="pl-c1">25</span>;</pre></div>
<p dir="auto">This causes the query plan to change, starting with NodeByLabelScan, then expanding and filtering. This query produces correct results.</p>