jeudi 25 mars 2021

Neo4j: Difference between rand() and rand() in with clause when matching random nodes

I found here that i can select random nodes from neo4j using next queries:

  1. MATCH (a:Person) RETURN a ORDER BY rand() limit 10
  2. MATCH (a:Person) with a, rand() as rnd RETURN a ORDER BY rnd limit 10

Both queries seems to do the same thing but when I try to match random nodes that are in relationship with a given node then I have different results:

The next query will return always the same nodes (nodes are not randomly selected)

MATCH (p:Person{user_id: '1'})-[r:REVIEW]->(m:Movie)
return m order by rand() limit 10

...but when I use rand() in a with clause I get indeed random nodes:

MATCH (p:Person{user_id: '1'})-[r:REVIEW]->(m:Movie)
with m, rand() as rnd
return m order by rnd limit 10

Any idea why rand() behave different in a with clause in the second query but in the first not?




Aucun commentaire:

Enregistrer un commentaire