Sun Java System Portal Server 7.1 Developer's Guide

Display the Results

The example application displays the query results to standard output or to a named file. In reality, you would do more with the results than just print them like this, but once you know how to get the results out of the Search object, it is up to you what you do with them. You can use standard Java functionality to process the results in any way you like.

The Search object has a method called getResultStream() that returns a SearchInputStream object. Each result is read from this Search stream in turn. Note that the client server connection uses an efficient streamed protocol; it is conceivable that the server is still returning later results while the client is processing the first results. For each Search object read from the result stream you can use the getValue() method to get the value of a particular field, for example, getValue("title") gets the title of a Search object.

First, print out some general result information:


System.out.println("=========================================");
System.out.println("page " + pagenum
+ ": hits " + search.getFirstHit()
+ " to " + (search.getFirstHit() + search.getResultCount() - 1)
+ " out of " + search.getHitCount()
+ " across " + search.getDocumentCount() + " documents");
System.out.println("=========================================");
System.out.println();

Now, retrieve each search hit from the result stream as Search objects and print its URL, title, description, and score to the output stream (either the Java console, standard output, or a named output file).


SearchInputStream resultStream = search.getResultStream();
Search search;
/* Examine the results of the search. The following
* code loops through the stream of Search instances. */
for (search = resultStream.readSearch(); search != null; 
search = resultStream.readSearch()) {
    // For illustration, dump out the entire Search on 
			the first page only.
    if (pagenum == 1)
        firstPageSearch.write(search.toByteArray());
        /* Now we use the getValue() method to get
        * the values of each of the requested
        * attributes. URL is special and has
        * its own accessor method.
        */
        String  u = search.getURL();
        String  t   = search.getValue("title");
        String  d   = search.getValue("description");
        String  sc  = search.getValue("score");

        /* do something with the results */
        System.out.println(
            "TITLE:       " + t  + "\\n" +
            "URL:         " + u  + "\\n" +
            "SCORE:       " + sc + "\\n" +
            "DESCRIPTION: " + d  + "\\n" +
            "--------------------------------------------\\n"
        );
        // If there is a Search output file, write the Search 
				data there too...
        if (sos != null) {
            try {
                sos.writeBytes(search.toString());
            }
            catch (Exception e1) {
                System.out.println("Error: failed to write to Search
                output file: " + e1);
            }
        }
    }
    // Break if the largest requested hit has been displayed
    if (search.getHitCount() <= (firstHit + pagesize - 1))
        break;
    }
    if (firstPageSearch == null)
        System.out.println("No matching documents found.");
    }