AEM Query builder get results in random order | Community
Skip to main content
Level 3
September 8, 2023

AEM Query builder get results in random order

  • September 8, 2023
  • 3 replies
  • 1940 views

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.

3 replies

bilal_ahmad
Level 5
September 8, 2023

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

Level 3
September 8, 2023

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.

sherinregi-1
Community Advisor
Community Advisor
September 8, 2023

@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

 

 

 

 

 

Level 3
September 8, 2023

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.

bilal_ahmad
Level 5
September 8, 2023

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

kautuk_sahni
Community Manager
Community Manager
September 8, 2023

@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. 

Kautuk Sahni