Sun Java System Portal Server 7 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 SOIFInputStream object. Each result is read from this SOIF 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 SOIF 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 SOIF 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 SOIF objects and print its URL, title, description, and score to the output stream (either the Java console, standard output, or a named output file).


SOIFInputStream resultStream = search.getResultStream();
SOIF soif;
/* Examine the results of the search. The following
* code loops through the stream of SOIF instances. */
for (soif = resultStream.readSOIF(); soif != null; soif = resultStream.readSOIF()) {
    // For illustration, dump out the entire SOIF on the first page only.
    if (pagenum == 1)
        firstPageSOIF.write(soif.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 = soif.getURL();
        String  t   = soif.getValue("title");
        String  d   = soif.getValue("description");
        String  sc  = soif.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 SOIF output file, write the SOIF data there too...
        if (sos != null) {
            try {
                sos.writeBytes(soif.toString());
            }
            catch (Exception e1) {
                System.out.println("Error: failed to write to SOIF
                output file: " + e1);
            }
        }
    }
    // Break if the largest requested hit has been displayed
    if (search.getHitCount() <= (firstHit + pagesize - 1))
        break;
    }
    if (firstPageSOIF == null)
        System.out.println("No matching documents found.");
    }