Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Aem results

Avatar

Level 3

Hi All, 

I have a requirement to get search results based on search term I was able to do that and categorie them based on tags but along with category I need to show total results

But for pagenation I'm using offset and limit in the query with same query we need to get total results of that is there any way?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

you can try like the below:

 

public SearchResult performSearch(String searchTerm, int offset, int limit, ResourceResolver resourceResolver) {
Map<String, String> map = new HashMap<>();
map.put("type", "cq:Page");
map.put("fulltext", searchTerm);
map.put("p.offset", String.valueOf(offset));
map.put("p.limit", String.valueOf(limit));

QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
SearchResult result = query.getResult();

// To GET Total number of search result matched
long totalMatches = result.getTotalMatches();

// Iterate over the hits
for (Hit hit : result.getHits()) {
// // Now iterate through hit and display the result
}

return result;
}

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

you can try like the below:

 

public SearchResult performSearch(String searchTerm, int offset, int limit, ResourceResolver resourceResolver) {
Map<String, String> map = new HashMap<>();
map.put("type", "cq:Page");
map.put("fulltext", searchTerm);
map.put("p.offset", String.valueOf(offset));
map.put("p.limit", String.valueOf(limit));

QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
SearchResult result = query.getResult();

// To GET Total number of search result matched
long totalMatches = result.getTotalMatches();

// Iterate over the hits
for (Hit hit : result.getHits()) {
// // Now iterate through hit and display the result
}

return result;
}