Most of the functionality lies in results.jsp
. Much of it is for paging the search
results, which we'll not cover here as it's commented well enough. The first thing in this page is
the actual imports for the Lucene classes and Lucene demo classes. These classes are loaded from
the jars included in the WEB-INF/lib
directory in the luceneweb.war
file.
You'll notice that this file includes the same header and footer as index.jsp
. From
there it constructs an IndexSearcher
with the
indexLocation
that was specified in configuration.jsp
. If there is an
error of any kind in opening the index, it is displayed to the user and the boolean flag
error
is set to tell the rest of the sections of the jsp not to continue.
From there, this jsp attempts to get the search criteria, the start index (used for paging) and the
maximum number of results per page. If the maximum results per page is not set or not valid then it
and the start index are set to default values. If only the start index is invalid it is set to a
default value. If the criteria isn't provided then a servlet error is thrown (it is assumed that
this is the result of url tampering or some form of browser malfunction).
The jsp moves on to construct a StandardAnalyzer
to
analyze the search text. This matches the analyzer used during indexing (IndexHTML
), which is generally
recommended. This is passed to the QueryParser
along with the
criteria to construct a Query
object. You'll also notice the string literal "contents"
included. This specifies
that the search should cover the contents
field and not the title
,
url
or some other field in the indexed documents. If there is any error in
constructing a Query
object an
error is displayed to the user.
In the next section of the jsp the IndexSearcher
is asked to search
given the query object. The results are returned in a collection called hits
. If the
length property of the hits
collection is 0 (meaning there were no results) then an
error is displayed to the user and the error flag is set.
Finally the jsp iterates through the hits
collection, taking the current page into
account, and displays properties of the Document
objects we talked about in
the first walkthrough. These objects contain "known" fields specific to their indexer (in this case
IndexHTML
constructs a document
with "url", "title" and "contents").
Please note that in a real deployment of Lucene, it's best to instantiate IndexSearcher
and QueryParser
once, and then
share them across search requests, instead of re-instantiating per search request.