Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

List<Search.Page> resultPages = result.getResultPages()

Avatar

Level 2

Hi - I want to understand how the  result.getResultPages(); work from wcm Search. Also how does resultPage.getURL() and resultPage.getIndex() works. All pointers and help appreciated

 

~Nitin

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi there,
the javadoc for this topic is actually quite good (http://wem.help.adobe.com/enterprise/en_US/10-0/wem/javadoc/com/day/cq/search/result/SearchResult.ht...).
Even better is to take a look at the standard search component, but I can try to digest it a bit from my experience.

getResultPages
When you have done a search with help of the components and classes inside AEM, you have access to the "result object" from the interface SearchResult.
This object has the great function getResultPages() which will return a normal java.util list (of ResultPages).
These pages can be explained as a collection of hits. Basically you set the number of hits you want per page and the search calculates how many pages that would result in.
It then arranges the hits on the required amount of pages. These pages can then later on be used for pagination the the search results and works just like any search function on e.g. Google. There you see that the results are presented, a few at a time, on different result pages and you can click on any page number to see more results.

resultPage.getURL()
Lets say the the search returned 30 hits and we wanted to display 10 results per page. Then we would automatically get 3 resultpages. For the search component to know which page we want to view we must supply the page it is located on with some kind of number. This is sometimes done by the following syntax  
"seach.html?q=searchtext&page=2" telling the component that we want to display the second page in this case.

When using the the resultPage, you will autmatically get the required url for the component by using resultPage.getURL(). This can then be used as a <a href ..> link to display the next set of hits. For example if we look at the standard search component is is quite neatly used like this:

To display the previus result page we first check if there is a previous page and the get that page from the result with the getPreviousPage(). Then we use the getURL() function on that page and use it as the link.

<c:if test="${result.previousPage != null}"> <a href="${result.previousPage.URL}"><fmt:message key="previousText"/></a> </c:if>

This goes for the next result page aswell which works in just the same way.
 

<c:if test="${result.nextPage != null}"> <a href="${result.nextPage.URL}"><fmt:message key="nextText"/></a> </c:if>


resultPage.getIndex()
This function simply returns the index or positon in the list which the result page is is. If its the third page, the getIndex() function will return 2, since that's the behvious of Lists in java. This is used in the standard component as follows:
 

<c:when test="${page.currentPage}">${page.index + 1}</c:when>

Here it checks if the resultpage is the current page and then displays the current page index + 1 to get the actual number as we would count it and not the index.

I hope this will help you in some way
/Johan

View solution in original post

4 Replies

Avatar

Level 2

Thanks a lot Johan. This explanation really helped. 

Avatar

Correct answer by
Level 7

Hi there,
the javadoc for this topic is actually quite good (http://wem.help.adobe.com/enterprise/en_US/10-0/wem/javadoc/com/day/cq/search/result/SearchResult.ht...).
Even better is to take a look at the standard search component, but I can try to digest it a bit from my experience.

getResultPages
When you have done a search with help of the components and classes inside AEM, you have access to the "result object" from the interface SearchResult.
This object has the great function getResultPages() which will return a normal java.util list (of ResultPages).
These pages can be explained as a collection of hits. Basically you set the number of hits you want per page and the search calculates how many pages that would result in.
It then arranges the hits on the required amount of pages. These pages can then later on be used for pagination the the search results and works just like any search function on e.g. Google. There you see that the results are presented, a few at a time, on different result pages and you can click on any page number to see more results.

resultPage.getURL()
Lets say the the search returned 30 hits and we wanted to display 10 results per page. Then we would automatically get 3 resultpages. For the search component to know which page we want to view we must supply the page it is located on with some kind of number. This is sometimes done by the following syntax  
"seach.html?q=searchtext&page=2" telling the component that we want to display the second page in this case.

When using the the resultPage, you will autmatically get the required url for the component by using resultPage.getURL(). This can then be used as a <a href ..> link to display the next set of hits. For example if we look at the standard search component is is quite neatly used like this:

To display the previus result page we first check if there is a previous page and the get that page from the result with the getPreviousPage(). Then we use the getURL() function on that page and use it as the link.

<c:if test="${result.previousPage != null}"> <a href="${result.previousPage.URL}"><fmt:message key="previousText"/></a> </c:if>

This goes for the next result page aswell which works in just the same way.
 

<c:if test="${result.nextPage != null}"> <a href="${result.nextPage.URL}"><fmt:message key="nextText"/></a> </c:if>


resultPage.getIndex()
This function simply returns the index or positon in the list which the result page is is. If its the third page, the getIndex() function will return 2, since that's the behvious of Lists in java. This is used in the standard component as follows:
 

<c:when test="${page.currentPage}">${page.index + 1}</c:when>

Here it checks if the resultpage is the current page and then displays the current page index + 1 to get the actual number as we would count it and not the index.

I hope this will help you in some way
/Johan

Avatar

Level 3

Hi,

It helps but also gives lot of problem when we have to define google like pagination. For example, I want to show 10 results per page and after page 6, I want to move the pagination numbers one by one. ie. first it will show 1 2 3 4.......10 and after page 6 is clicked, it need to show 2 3 4 5 6 7 8 9 10 11 and then for page 11 is clicked then 5 6 7 8 9 10 11 12 13 14 etc. We tried implementing this but this was too complexed as we tried to do as showed below.

private List<ResultPage> extractSubList(SearchResult result){
        long currentIndex = result.getStartIndex() / result.getHitsPerPage();
        int startIndex = (int)currentIndex - 5;
        int endIndex = (int)startIndex + 10;
        startIndex = startIndex < 0 ? 0 : startIndex;
        endIndex = endIndex >= result.getResultPages().size() ? result.getResultPages().size() : endIndex;
        List<ResultPage> subList = result.getResultPages().subList(startIndex, endIndex);
        return subList;
    }

Though this logic works fine for us till 11th page but as soon as we move to 12th page, the resultPage list index changes and also the result page size changes and hence we could not provide proper start and end indexes. Also we tried to restrict 10 numbers to display but could do that.

Can you please help me in acheiving this functionality? Let me know where we are doing wrong.

Avatar

Employee Advisor

Hi,

a 100% correct pagination is nearly impossible, especially under a constraint, that exact values are given.

Because it's not sufficent just to check how many matches you have in the index, but you need to filter every search result and check, if it passes access control. And to get the complete number of hits, you cannot use a lazy search, but execute the complete search as a whole to count the result set. So if you want to deliver the ultimate correct list of results it can take very long time.

You should ask yourself, if a pagination is really needed. My personal search experience is, that if I don't find a good search result within the first 2 response pages, I refine the search. I don't care if I get 100 hits or 100k hits. In every case it's to much to deal with as human.

kind regards,
Jörg