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?
Solved! Go to Solution.
Views
Replies
Total Likes
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;
}
You can try using guessTotal which is also the recommended approach for pagination.
https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/pla...
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;
}
Views
Likes
Replies