Sun Java System Portal Server 7.1 Developer's Guide

Execute the Search Query

An output stream is created to hold the search results and paginate through the search results for a fixed number of pages, in this case five pages in total, where each page has viewHits (=20) results. The first page starts with the first hit (firstHit=1). The search is executed again for each page of results. It is possible to cache the results for all pages with a single search of course, but it is often easier to simply resubmit the search each time. This is equivalent to a user clicking a next button in a search user interface.


/* Execute the query. */
System.out.println("\\nSearch results for ’" + scope + "’");
 DataOutputStream sos = null;
if (SearchOutputFile != null) {
try {
sos = new DataOutputStream(new FileOutputStream(SearchOutputFile));
}
catch (Exception e1) {
System.out.println("Error: failed to create output file: " + e1);
}
}
int  pagenum = 1;
int  pagesize = 10;
SearchBuffer firstPageSearch = new SearchBuffer();
for (; pagenum <= 5; pagenum++) {
int firstHit = (pagenum-1)*pagesize+1;
try {
search.doQuery(firstHit, pagesize);
}
catch (Exception ex) {
ex.printStackTrace();
break;
}
// Check the result count. -1 indicates an error.
if (search.getResultCount() <= 0)
break;

The results are stored in the Search object. Now do something with the results. The functions doSomethingWithResults() and displayHTMLResults() will be defined in this file. They each show a different way of extracting the results from the Search object.