2
votes

I'm trying to limit my results to a randomized set. So far, I have this query working:

MATCH (n),
RETURN n, rand() as random
ORDER BY random
LIMIT 25

However, when trying to replace LIMIT 25 with a random number, things go wrong, in both of the following two cypher examples:

MATCH (n)
RETURN n, rand() as random, toInt(rand()*25) as randCount
ORDER BY random
LIMIT randCount

In the above, removing LIMIT returns randcount correctly

WITH toInt(rand()*25) as randCount
MATCH (n)
RETURN n, rand() as random
ORDER BY random
LIMIT randCount

My immediate thought is that Cypher doesn't (yet) support using a variable/expression to limit results... Or I'm doing it wrong ;-)

1
This is something that's scheduled for development but not done yet :) - Michael Hunger
Sounds great @MichaelHunger! - rdiz

1 Answers

2
votes

I tried your examples and you're right, cypher doesn't support variable as argument for LIMIT.

Here is the proof you're looking for:

I started the above query:

PROFILE 
MATCH (n)
RETURN n, rand() as random, toInt(rand()*25) as randCount
ORDER BY random
LIMIT 1

Here is the result:

PROFILE Result

As you can see on the profile result, LIMIT is executed before randCount is affected, so when you do LIMIT randCountrandCount doesn't exist, like every other variables you created.

Even if random is done in the Projection part, its value is not affected to randCount immediately.