Pick random node

Hello Dgraph team,

Picking a random has already been mentioned here and on github, but no feature request ticket seems to have been created.

I’m still in the need of such a feature as it would greatly improve:

  • Performances because I currently need to do random queries twice (in a transaction): First to count nodes, second to set one or more random offsets. My biggest and slowest query right now is to do matchmaking in my game, and I have to do it twice each time.
  • Code readability because my current solution requires a big function.

The syntax could be similar to first and offset keywords: random: 3 which would mean we want to pick 3 different random nodes from the current result set. It would perfectly work with offset and first to select the range before picking random nodes.

Please consider my request, I’m sure many projects would benefit from it :slightly_smiling_face:

1 Like

This may be a side effect of fixing Inconsistent response of sort as a result of indexing of a predicate when we get round to it.

@chewxy it would be very helpful for me to pick random nodes.

You know what, let’s make this a proper feature request. Accepted.

1 Like

Once we have a count, we can pick random nodes from the client side. Right now there is no way to know what the maximum node is to provide a random offset to a maximum value.

This of course would be limited to picking a random group of N nodes and not N random nodes. The use case needing N random nodes could be worked around by doing N requests which could be within a single query.

Indeed that’s pretty much what I’m doing right now:

  • A first request to count available nodes according to my filters
  • A second request, in the same transaction, with same filters, and split into query blocks, as many as required random nodes, each having a random offset.

oh, yes, sorry this is DQL tagged and not GQL. I was in the GQL mindset not having count.

Be aware that doing the second read query in the same transaction may fail when there have been a hand full of mutations in the meanwhile. After some mutations a rollup happens which makes the startTs of your transaction disappear.

Sure, you can retry the two steps until you succeed, but just so you know that this situation can occur with concurrent writes.

Indeed, I do this way because this is currently the only solution I found out.
That’s precisely why I’m requesting for a random nodes picker.

Duplicate: Is there a rand option for dgraph?

Hello the team, and Happy New Year to you :slight_smile:
Is there any update on this feature request?

At the moment we’re pretty busy on shoring up some older features. Sorry. I’ll try my best to get this in

1 Like