Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

v1101
Level 5
Level 5

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.

 

 

1 Accepted Solution
BrianKasingli
Correct answer by
Community Advisor
Community Advisor

@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);
}

 

 

View solution in original post

3 Replies
Vaibhavi_Gowda
Community Advisor
Community Advisor

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

BrianKasingli
Correct answer by
Community Advisor
Community Advisor

@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);
}

 

 

View solution in original post

SundeepKatepally
Level 5
Level 5

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