sanity-io / GROQ

Specification for GROQ - The Query Language for JSON
https://groq.dev
MIT License
398 stars 15 forks source link

Feature Request: Random Document Retrieval in GROQ #118

Open tamas-palecian opened 8 months ago

tamas-palecian commented 8 months ago

**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:

order(random()): Orders the results based on a random seed.
order(shuffle()): Shuffles the results to return them in a non-deterministic order.

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.