-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Random Document Retrieval in GROQ #118
Comments
This was briefly discussed already here. He makes a good point in this comment: how would random sorting affect caching? |
The simplest way of providing some randomness while still supporting caching: Add support for a |
This could work but basically delegates the responsibility to the final user. I can totally see new developers using big numbers just for the sake of it, resulting in less cache hit rate. I don't know whether or not this aligns with the team's view as they might prefer a more consistant strategy, even if that came with less cache hit rates. However I think that having a built-in function for this purpose would be more efficient than using client-side solutions: a client-side workaround I have found is, for example, to have a random index value (possibly normally distributed) in each document, then generating a random number client-side and fetching the first document that presents a larger index.
{
"_id": "doc1",
"_type": "document",
"title": "Document 1",
"randomIndex": 0.42
},
{
"_id": "doc2",
"_type": "document",
"title": "Document 2",
"randomIndex": 0.78
},
{
"_id": "doc3",
"_type": "document",
"title": "Document 3",
"randomIndex": 0.15
},
{
"_id": "doc4",
"_type": "document",
"title": "Document 4",
"randomIndex": 0.63
}
|
**Is your feature request related to a problem?
Currently, GROQ lacks a built-in mechanism to fetch documents in a random order.
This limitation complicates scenarios where displaying randomised content is essential, such as in creating dynamic, engaging user experiences. Developers must resort to custom, often cumbersome, workarounds to achieve this functionality, which can increase development time and complexity.
Describe the solution you'd like
Introduce a straightforward method for random sorting within GROQ queries.
This enhancement could be implemented with simple function calls within the order clause, examples of which might include:
Describe alternatives you've considered
While custom workarounds exist, they often involve fetching more documents than necessary and shuffling them client-side or periodically updating a "random" field in each document to sort by. These methods are not as efficient or straightforward as having native support for randomness in GROQ queries.
Additional context
Random document retrieval is a common requirement for many applications, including content sites looking to showcase a "random article of the day," e-commerce platforms featuring random products, or any service aiming to enhance user engagement by providing a non-static experience. Native support for this feature in GROQ would significantly simplify development workflows and enable more dynamic content delivery strategies.
The text was updated successfully, but these errors were encountered: