Fetch random items

So I was trying to fetch random items from the API but I don't find any way.
As a workaround, we fetch all items and then only pick a few of them.
This is far from ideal, but can't find any other solution.

Is it possible to fetch random items from Prismic?

Thank you.

Hi @jose.varela,

Thanks for this question! It's an interesting use case. I would get the total number of docs in the repo with a general query, then generate a random number between 1 and the total, and then grab a doc at that index using API pagination. Here's a repl showing how that could work:

Let me know if that makes sense, or if you have any other questions :slight_smile:

Best,
Sam

Hey @samlittlefair,
Interesting approach, but in my use case I would need 4 random items.

I could make 4 parallel requests with the approach you presented, but seems ugly.

Is there any other way around?

Hey @jose.varela,

I can't think of any other method.

Personally, I would go ahead and make the four requests in parallel. They will probably use the same ref, so the data will be cached and the requests will be extremely fast. Though, I see what you mean that it is a little ugly, and you'll need to guard against duplicates.

In any case, I'll change this to a feature request for orderings=random, and submit it to our product team.

Sam

This is being tracked as an open feature request.

If you have another use-case for this feature, you can 'Flag' this topic to reopen. Please use the :heart: button to show your support for the feature and check out our Feature Request Guidelines.