Inconsistent cypher query query in neo4j
To illustrate this problem, create thousands of z-labeled nodes by increasing the numeric zid attribute.
FOREACH (i IN range(1, 1000)| CREATE (z:z { zid: i }));
Now find the node using a random zid between 1 and 1000.
MATCH (n:z { zid: round(rand()*1000)})
RETURN n;
The above cypher returns inconsistent results, sometimes no nodes are returned, sometimes multiple nodes are returned.
Tweaking the cypher as follows gives consistent results.
WITH round(rand()*1000) AS x
MATCH (n:z { zid: x })
RETURN x, n;
What's wrong with the first cypher request?
The reason you get inconsistent results with the first query is due to the way Neo4j evaluates Cypher queries. The function round(rand()*1000)
is evaluated for each of the index elements of the label for z
using WHERE
or shorthand syntax. When you use a sentence WITH
, the feature is evaluated once.
That being said, it looks like a function-specific error rand()
.