Skip to main content
Level 3
Sep 8, 2023 4:54 AM UTC

AEM Query builder get results in random order

Hello All,

 

We have a requirement where we need to get the results in a random order (we don't need any sorting order)

Query should return the results in random.

 

 

Any thoughts here?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

8 replies

bilal_ahmad
Level 5
Sep 8, 2023 5:12 AM UTC

Hi @sateeshkreddy 

 

There's nothing called 'no sorting order' You either specify the order or by default it's sorted by jcr:score

 

thanks,

Bilal

Translate
Level 3
Sep 8, 2023 6:06 AM UTC

Thanks for the response.

If we go with jcr: score, it will return the results in the same order. As of now, we are explicitly shuffling the results once we get the query result. I am looking for a solution, if we have any in the OOTB of the query builder.

Translate
sherinregi-1
Community Advisor
Community Advisor
Sep 8, 2023 5:19 AM UTC

@sateeshkreddy 

I don't think you have anything OOTB to get random order . However you can use collection to sort it random . Please follow the below community post

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/jcr-results-order-by-random-or-every-query-must-give-results-in/td-p/373544

 

 

 

 

 

Translate
Level 3
Sep 8, 2023 6:09 AM UTC

Thaks for the response.

Our implementation as of today : On page load we are loading 9 items and on click of load more we will again call the servlet which inturn calls the query builder with next 9 items and so on.

The problem here is we are getting the same 9 items everytime and shuffling and sending back to Frontend . Now we want to shuffle entire collection and send back 9 items at each time from the backend.

Translate
bilal_ahmad
Level 5
Sep 8, 2023 6:28 AM UTC

I'd suggest you to shuffle in your front end before showing the next items(when you click on show more).

 

So, fetch all the results and shuffle on every 'load more' click in your front end to make sure you achieve what's required.

 

Or you can utilise shuffle(): method from Collections library in Java to achieve the same.

 

Thanks,

Bilal

Translate
kautuk_sahni
Level 9
Sep 8, 2023 9:49 AM UTC

@sateeshkreddy Do you find the suggestions from users useful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. 

Translate
Kautuk Sahni