Add ability to sort by random #3240
Replies: 8 comments 9 replies
This comment has been hidden.
This comment has been hidden.
-
It is a feature that I would need too, maybe with the possibility of passing a seed to maintain pagination. |
Beta Was this translation helpful? Give feedback.
-
@benhaynes @rijkvanzanten Is it something that you guys think to implement in a near future? That would be very dope 🙏 |
Beta Was this translation helpful? Give feedback.
-
I thought I'd chime in since I needed this feature as well. For anyone interested this is my solution using two queries. This only works if you need exactly one random item (as requested in the OP):
I believe on large tables this is performing better than a If you need more than 1 item I think you can still do what @erondpowell suggested - more or less. Let's say you need X items. If you generate exactly X random ids between min and max you'll likely have issues as mentioned by @rijkvanzanten. But if you generate a lot more than X ids (e.g. 10*X ids) and query with a ".. where id in {ids}" (I believe something like |
Beta Was this translation helpful? Give feedback.
-
I've given it a shot to implement this in an extension 🤓 https://github.com/br41nslug/directus-extension-randomized |
Beta Was this translation helpful? Give feedback.
-
Heya! Thanks for opening this feature request! This feature request has received over 15 votes from the community. This means we'll move this feature request to the Under Review state! The Core team will schedule a meeting to review this request as soon as possible. The discussion will then be approved or denied. You may or may not be invited to join this meeting with the core team. For more information, see our Feature Request Process. |
Beta Was this translation helpful? Give feedback.
-
I would like to report here some consideration I've made while checking out that nice extension from @br41nslug. While we all agree that the best approach to this would be to use some database native functions (eg.: `ORDER BY RANDOM()' for pg) we also know that not all database types have these type of function, so external one needs to be created. But those external ones have one thing in common: they usually need to know the entirety of the data to shuffle the array. When shuffling an array one of the first solution that comes in mind is to create an empty array, pick an item at a random index, move it to the empty array and removing it from the original one via something like Another rather small, but important topic is about shuffling a limited portion of a particular collection. The first intuition could be to fetch all the available ids and pick randomly a subset up to the desired amount, and then fetch a second time the actual data with that array of those randomly picked ids (this way it will go easier on relationships). This second fetch should also be randomized, and the reason being is easier to understand if we see the collection as an array of all the letters in the latin alphabet, When randomly picking ids we are effectively randomly picking single letters, but once we fetch the actual data using those ids we do receive a sorted (based on collection preferences) array. This will result in a lower performance, but still, if we use in-place shuffling for both random picking and final shuffling, we should see performance between A practical use of this, with some more information, could be checked in this PR. Things that still need an answer/research:
|
Beta Was this translation helpful? Give feedback.
-
Me neither
Exactly! Those I've described are just workaround for those database vendors that do not have support for this task. But being just workarounds I consider them ok for an extension but not good enough as Directus first party functions. The inconvenient part is that, humanly speaking, the randomness of an array is just an non-expected order, but to effectively do it you must have an idea of the entirety of the dataset, or at least of that element that is perceived as the ordering value (either being sequential ids or sort field). In this case we could use that element that we already know it will be in that particular, randomize that order (maybe with a seed to handle pagination) and then do a single fetch to the related data. Effectively making that element the id of the item. I even find #13041 easier to deal with compared to this one... |
Beta Was this translation helpful? Give feedback.
-
Currently, I want to get a random item from the collection, can we do that?
I tried
sort=?
but seems to not work.Beta Was this translation helpful? Give feedback.
All reactions