<ahref="#Location of the source">Location of the source</a>
</li>
<li>
<ahref="#IndexFiles">IndexFiles</a>
</li>
<li>
<ahref="#Searching Files">Searching Files</a>
</li>
<li>
<ahref="#The Web example...">The Web example...</a>
</li>
</ul>
</div>
<aname="N10013"></a><aname="About the Code"></a>
<h2class="boxed">About the Code</h2>
<divclass="section">
<p>
In this section we walk through the sources behind the command-line Lucene demo: where to find them,
their parts and their function. This section is intended for Java developers wishing to understand
how to use Lucene in their applications.
</p>
</div>
<aname="N1001C"></a><aname="Location of the source"></a>
<h2class="boxed">Location of the source</h2>
<divclass="section">
<p>
Relative to the directory created when you extracted Lucene or retrieved it from Subversion, you
should see a directory called <spanclass="codefrag">src</span> which in turn contains a directory called
<spanclass="codefrag">demo</span>. This is the root for all of the Lucene demos. Under this directory is
<spanclass="codefrag">org/apache/lucene/demo</span>. This is where all the Java sources for the demos live.
</p>
<p>
Within this directory you should see the <spanclass="codefrag">IndexFiles.java</span> class we executed earlier.
Bring it up in <spanclass="codefrag">vi</span> or your editor of choice and let's take a look at it.
</p>
</div>
<aname="N10037"></a><aname="IndexFiles"></a>
<h2class="boxed">IndexFiles</h2>
<divclass="section">
<p>
As we discussed in the previous walk-through, the <spanclass="codefrag">IndexFiles</span> class creates a Lucene
Index. Let's take a look at how it does this.
</p>
<p>
The first substantial thing the <spanclass="codefrag">main</span> function does is instantiate <spanclass="codefrag">IndexWriter</span>. It passes the string
"<spanclass="codefrag">index</span>" and a new instance of a class called <spanclass="codefrag">StandardAnalyzer</span>.
The "<spanclass="codefrag">index</span>" string is the name of the filesystem directory where all index information
should be stored. Because we're not passing a full path, this will be created as a subdirectory of
the current working directory (if it does not already exist). On some platforms, it may be created
in other directories (such as the user's home directory).
</p>
<p>
The <spanclass="codefrag">IndexWriter</span> is the main
class responsible for creating indices. To use it you must instantiate it with a path that it can
write the index into. If this path does not exist it will first create it. Otherwise it will
refresh the index at that path. You can also create an index using one of the subclasses of <spanclass="codefrag">Directory</span>. In any case, you must also pass an
instance of <spanclass="codefrag">org.apache.lucene.analysis.Analyzer</span>.
</p>
<p>
The particular <spanclass="codefrag">Analyzer</span> we
are using, <spanclass="codefrag">StandardAnalyzer</span>, is
little more than a standard Java Tokenizer, converting all strings to lowercase and filtering out
useless words and characters from the index. By useless words and characters I mean common language
words such as articles (a, an, the, etc.) and other strings that would be useless for searching
(e.g. <b>'s</b>) . It should be noted that there are different rules for every language, and you
should use the proper analyzer for each. Lucene currently provides Analyzers for a number of
different languages (see the <spanclass="codefrag">*Analyzer.java</span> sources under <ahref="http://svn.apache.org/repos/asf/lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/">contrib/analyzers/src/java/org/apache/lucene/analysis</a>).
</p>
<p>
Looking further down in the file, you should see the <spanclass="codefrag">indexDocs()</span> code. This recursive
function simply crawls the directories and uses <spanclass="codefrag">FileDocument</span> to create <spanclass="codefrag">Document</span> objects. The <spanclass="codefrag">Document</span> is simply a data object to
represent the content in the file as well as its creation time and location. These instances are
added to the <spanclass="codefrag">indexWriter</span>. Take a look inside <spanclass="codefrag">FileDocument</span>. It's not particularly
complicated. It just adds fields to the <spanclass="codefrag">Document</span>.
</p>
<p>
As you can see there isn't much to creating an index. The devil is in the details. You may also
wish to examine the other samples in this directory, particularly the <spanclass="codefrag">IndexHTML</span> class. It is a bit more
complex but builds upon this example.
</p>
</div>
<aname="N100AE"></a><aname="Searching Files"></a>
<h2class="boxed">Searching Files</h2>
<divclass="section">
<p>
The <spanclass="codefrag">SearchFiles</span> class is
quite simple. It primarily collaborates with an <spanclass="codefrag">IndexSearcher</span>, <spanclass="codefrag">StandardAnalyzer</span>
(which is used in the <spanclass="codefrag">IndexFiles</span> class as well) and a
<spanclass="codefrag">QueryParser</span>. The
query parser is constructed with an analyzer used to interpret your query text in the same way the
documents are interpreted: finding the end of words and removing useless words like 'a', 'an' and
'the'. The <spanclass="codefrag">Query</span> object contains
the results from the <spanclass="codefrag">QueryParser</span> which is passed to
the searcher. Note that it's also possible to programmatically construct a rich <spanclass="codefrag">Query</span> object without using the query
parser. The query parser just enables decoding the <ahref="queryparsersyntax.html">Lucene query