简体   繁体   中英

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?

It's important to understand that using rand() in the ORDER BY like this isn't doing what you think it's doing. It's not picking a random number per row, it's ordering by a single number.

It's similar to a query like:

MATCH (p:Person)
RETURN p
ORDER BY 5

Feel free to switch up the number. In any case, it doesn't change the ordering because ordering every row, when the same number is used, doesn't change the ordering.

But when you project out a random number in a WITH clause per row, then you're no longer ordering by a single number for all rows, but by a variable which is different per row.

Your assumption is not correct. I always get a randomized order by using below query when running it in Neoj Desktop.

MATCH (p:Person{name: 'Jessica Thompson'})-[r:REVIEWED]->(m:Movie)
return m order by rand() limit 10

If your data has only 5 records in it, then adding a limit 10 will ALWAYS return 5 records. Put a limit of 2 then you will see it is "randomized"

MATCH (p:Person{name: 'Jessica Thompson'})-[r:REVIEWED]->(m:Movie)
return m order by rand() limit 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM