Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

AEM Query builder get results in random order

Avatar

Level 3

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?

 

8 Replies

Avatar

Community Advisor

Hi @sateeshreddy 

 

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

 

thanks,

Bilal

Avatar

Level 3

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.

Avatar

Level 8

@sateeshreddy 

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

 

 

 

 

 

Avatar

Level 3

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.

Avatar

Community Advisor

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

Avatar

Level 8

@sateeshreddy 

What the your approximate result set size. Just wanted to see the impact if you fetch all the results with limit set to -1 and then store the results in a collection in heap memory where you can use the collection utilities.

 

 

Avatar

Community Advisor

Hello @sateeshreddy 

 

Its interesting use-case. Why would one shuffle results?

 

Just curious, as shuffling means performance bottle necks

- One for getting all results at once

- Consuming memory to maintain these. Lot of resources used, if its different for each user

- Shuffling

- Since, its dynamic, means no caching on dispatcher/CDN

 

Rather than shuffling, you can probably create multiple selectors to map against specific properties. Let UI choose a selector randomly, and request. Fetch results base on these pre-defined selector->property mapping. Benefits:

- Responses for all selectors can be cached.

- Using offset, get unique results for each page

 


Aanchal Sikka

Avatar

Administrator

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