cancel
Showing results for 
Search instead for 
Did you mean: 

Pagination of search results in a search web script

theorbix
Confirmed Champ
Confirmed Champ
We’re developing a custom web application for Alfresco.

The application must perform queries on the Alfresco repository using a set of custom attributes and return the result set to the browser.

We have developed a custom web script in JavaScript, that performs the query and returns the search results in HTML (the final version will likely return the results in JSON). See the attached zip file for the source code.

The question now is how to perform the pagination of search results to improve performances.

We have seen that the standard /api/search Web Script provides a parameter for pagination: &p={startPage?}

GET /alfresco/service/search/keyword.html?q={searchTerms}&p={startPage?}&c={count?}&l={language?} 

The Web Script above uses a Java-backed component… Can the same result be achieved in a custom Javascript-backed  component? And what is the correct JavaScript to “fetch” search results in a paginated way?
3 REPLIES 3

theorbix
Confirmed Champ
Confirmed Champ
To better explain what we're doing, here is the full source of the web script.

This is the javascript script:


var query = "";
var custom = "@custom\\:";
var found = "0";
for (arg in args) {
   if(args[arg]!=null && args[arg].length > 0 )
   {
      found="1";
      logger.log(args[arg]);
       query += (custom + arg + ":" + args[arg]+" ");
   }
}

if(found.equals("1"))
   query= " AND "+ query;

var luceneQuery = " +PATH:\"/app:company_home/cm:MyCusotmer//*\"" + query + " AND TYPE:\"custom:DamType\"";

var nodes = search.luceneSearch(luceneQuery);
model.total = nodes.length;
model.resultset = nodes;

This is the code of the FreeMarker script that returns the output in HTML:

<table>
   <#list resultset as node>
        <tr>    
              <td>
<a href="${url.serviceContext}/api/node/content/${node.nodeRef.storeRef.protocol}/${node.nodeRef.storeRef.identifier}/${node.nodeRef.id}/${node.name?url}?alf_ticket=${session.ticket}">
${node.name}
</a>
         </tr>
   </#list>
</table>

The issue is that the model.resultset = nodes; statement at the end of the JavaScript returns to the FreeMarker script the whole result set… how can I return just a set of nodes (e.g. from 1 to 10, from 11 to 20… etc)?

steventux
Champ in-the-making
Champ in-the-making
You can splice the nodes array with the start index and page size.

nodes.splice(args['start'], args['max']);

damiar
Champ in-the-making
Champ in-the-making
Alfresco doesn't provide any way of pagination. You must to paginate by yourself, sending to the webscript arguments like "count" and "startIndex".