JCR results order by random or every query must give results in random order | Community
Skip to main content
Level 5
Aug 6, 2020 1:04 AM UTC
Solved

JCR results order by random or every query must give results in random order

Team,

I had another query opened but I was closed.

 

 how to query JCR to get results in random order. For every query of JCR, the results should be in random order. Like in SQL where there is order by rand(). Could someone help with this.

 

path=/content/we-retail
1_property=sling:resourceType
1_property.value=summer/components/page/activities
1_property.operation=like

The order by is not by a specific field but by random number.

Thanks.

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
BrianKasingli
BrianKasingliAccepted solution
New Participant
Aug 6, 2020 1:05 PM UTC

@v1101, 

Please try with the code provided:

 

PredicateGroup group = new PredicateGroup(); group.add(new Predicate("mypath", "path").set("path", "/content")); group.add(new Predicate("mytype", "type").set("type", "nt:file")); Query query = queryBuilder.createQuery(group, session); if (query != null) { List<hit> list = result.getHits(); Collections.shuffle(list); }

 

 

4 replies

aanchal-sikka
New Participant
Sep 8, 2023 8:45 AM UTC

Hello @v1101 

 

What you are trying to achieve  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

Translate
Aanchal Sikka
SundeepKatepally
New Participant
Aug 7, 2020 2:42 AM UTC

Order isn't guaranteed.

The default search implementation of Apache Lucene returns results sorted by score (the most relevant result first), then by id (the oldest result first).

Apache Lucene - Scoring

Translate
BrianKasingli
BrianKasingliAccepted solution
New Participant
Aug 6, 2020 1:05 PM UTC

@v1101, 

Please try with the code provided:

 

PredicateGroup group = new PredicateGroup(); group.add(new Predicate("mypath", "path").set("path", "/content")); group.add(new Predicate("mytype", "type").set("type", "nt:file")); Query query = queryBuilder.createQuery(group, session); if (query != null) { List<hit> list = result.getHits(); Collections.shuffle(list); }

 

 

Translate
Vaibhavi_J
Level 7
Aug 6, 2020 3:39 AM UTC

Hi @v1101 , 

jcr queries doesn't have option for randomizer but you have an option to achieve the outcome using collection. Because jcr result set is a collection and in collection you have option to shuffle the result. 

Please check the below thread for more details. 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/retrieve-random-images-from-jcr/qaq-p/225553?profile.language=en&pageRefresh=true

Translate