| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
<html> |
| 18 |
<head> |
| 19 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 20 |
<title>Apache Lucene - Building and Installing the Basic Demo</title> |
| 21 |
</head> |
| 22 |
<body> |
| 23 |
<p>The demo module offers simple example code to show the features of Lucene.</p> |
| 24 |
<h1>Apache Lucene - Building and Installing the Basic Demo</h1> |
| 25 |
<div id="minitoc-area"> |
| 26 |
<ul class="minitoc"> |
| 27 |
<li><a href="#About_this_Document">About this Document</a></li> |
| 28 |
<li><a href="#About_the_Demo">About the Demo</a></li> |
| 29 |
<li><a href="#Setting_your_CLASSPATH">Setting your CLASSPATH</a></li> |
| 30 |
<li><a href="#Indexing_Files">Indexing Files</a></li> |
| 31 |
<li><a href="#About_the_code">About the code</a></li> |
| 32 |
<li><a href="#Location_of_the_source">Location of the source</a></li> |
| 33 |
<li><a href="#IndexFiles">IndexFiles</a></li> |
| 34 |
<li><a href="#Searching_Files">Searching Files</a></li> |
| 35 |
<li><a href="#Embeddings">Working with vector embeddings</a></li> |
| 36 |
</ul> |
| 37 |
</div> |
| 38 |
<a id="About_this_Document"></a> |
| 39 |
<h2 class="boxed">About this Document</h2> |
| 40 |
<div class="section"> |
| 41 |
<p>This document is intended as a "getting started" guide to using and running |
| 42 |
the Lucene demos. It walks you through some basic installation and |
| 43 |
configuration.</p> |
| 44 |
</div> |
| 45 |
<a id="About_the_Demo"></a> |
| 46 |
<h2 class="boxed">About the Demo</h2> |
| 47 |
<div class="section"> |
| 48 |
<p>The Lucene command-line demo code consists of an application that |
| 49 |
demonstrates various functionalities of Lucene and how you can add Lucene to |
| 50 |
your applications.</p> |
| 51 |
</div> |
| 52 |
<a id="Setting_your_CLASSPATH"></a> |
| 53 |
<h2 class="boxed">Setting your CLASSPATH</h2> |
| 54 |
<div class="section"> |
| 55 |
<p>First, you should <a href= |
| 56 |
"http://www.apache.org/dyn/closer.cgi/lucene/java/">download</a> the latest |
| 57 |
Lucene distribution and then extract it to a working directory.</p> |
| 58 |
<p>You need four JARs: the Lucene JAR, the queryparser JAR, the common analysis JAR, and the Lucene |
| 59 |
demo JAR. You should see the Lucene JAR file in the core/ directory you created |
| 60 |
when you extracted the archive -- it should be named something like |
| 61 |
<span class="codefrag">lucene-core-{version}.jar</span>. You should also see |
| 62 |
files called <span class="codefrag">lucene-queryparser-{version}.jar</span>, |
| 63 |
<span class= |
| 64 |
"codefrag">lucene-analysis-common-{version}.jar</span> and <span class= |
| 65 |
"codefrag">lucene-demo-{version}.jar</span> under queryparser, analysis/common/ and demo/, |
| 66 |
respectively.</p> |
| 67 |
<p>Put all four of these files in your Java CLASSPATH.</p> |
| 68 |
</div> |
| 69 |
<a id="Indexing_Files"></a> |
| 70 |
<h2 class="boxed">Indexing Files</h2> |
| 71 |
<div class="section"> |
| 72 |
<p>Once you've gotten this far you're probably itching to go. Let's <b>build an |
| 73 |
index!</b> Assuming you've set your CLASSPATH correctly, just type:</p> |
| 74 |
<pre> |
| 75 |
java org.apache.lucene.demo.IndexFiles -docs {path-to-lucene} |
| 76 |
</pre> |
| 77 |
This will produce a subdirectory called <span class="codefrag">index</span> |
| 78 |
which will contain an index of all of the Lucene source code. |
| 79 |
<p>To <b>search the index</b> type:</p> |
| 80 |
<pre> |
| 81 |
java org.apache.lucene.demo.SearchFiles |
| 82 |
</pre> |
| 83 |
You'll be prompted for a query. Type in a gibberish or made up word (for example: |
| 84 |
"supercalifragilisticexpialidocious"). |
| 85 |
You'll see that there are no maching results in the lucene source code. |
| 86 |
Now try entering the word "string". That should return a whole bunch |
| 87 |
of documents. The results will page at every tenth result and ask you whether |
| 88 |
you want more results.</div> |
| 89 |
<a id="About_the_code"></a> |
| 90 |
<h2 class="boxed">About the code</h2> |
| 91 |
<div class="section"> |
| 92 |
<p>In this section we walk through the sources behind the command-line Lucene |
| 93 |
demo: where to find them, their parts and their function. This section is |
| 94 |
intended for Java developers wishing to understand how to use Lucene in their |
| 95 |
applications.</p> |
| 96 |
</div> |
| 97 |
<a id="Location_of_the_source"></a> |
| 98 |
<h2 class="boxed">Location of the source</h2> |
| 99 |
<div class="section"> |
| 100 |
<p>The files discussed here are linked into this documentation directly: |
| 101 |
<ul> |
| 102 |
<li><a href="src-html/org/apache/lucene/demo/IndexFiles.html">IndexFiles.java</a>: code to create a Lucene index. |
| 103 |
<li><a href="src-html/org/apache/lucene/demo/SearchFiles.html">SearchFiles.java</a>: code to search a Lucene index. |
| 104 |
</ul> |
| 105 |
</div> |
| 106 |
<a id="IndexFiles"></a> |
| 107 |
<h2 class="boxed">IndexFiles</h2> |
| 108 |
<div class="section"> |
| 109 |
<p>As we discussed in the previous walk-through, the <a href= |
| 110 |
"src-html/org/apache/lucene/demo/IndexFiles.html">IndexFiles</a> class creates |
| 111 |
a Lucene Index. Let's take a look at how it does this.</p> |
| 112 |
<p>The <span class="codefrag">main()</span> method parses the command-line |
| 113 |
parameters, then in preparation for instantiating |
| 114 |
{@link org.apache.lucene.index.IndexWriter IndexWriter}, opens a |
| 115 |
{@link org.apache.lucene.store.Directory Directory}, and |
| 116 |
instantiates {@link org.apache.lucene.analysis.standard.StandardAnalyzer StandardAnalyzer} |
| 117 |
and {@link org.apache.lucene.index.IndexWriterConfig IndexWriterConfig}.</p> |
| 118 |
<p>The value of the <span class="codefrag">-index</span> command-line parameter |
| 119 |
is the name of the filesystem directory where all index information should be |
| 120 |
stored. If <span class="codefrag">IndexFiles</span> is invoked with a relative |
| 121 |
path given in the <span class="codefrag">-index</span> command-line parameter, |
| 122 |
or if the <span class="codefrag">-index</span> command-line parameter is not |
| 123 |
given, causing the default relative index path "<span class= |
| 124 |
"codefrag">index</span>" to be used, the index path will be created as a |
| 125 |
subdirectory of the current working directory (if it does not already exist). |
| 126 |
On some platforms, the index path may be created in a different directory (such |
| 127 |
as the user's home directory).</p> |
| 128 |
<p>The <span class="codefrag">-docs</span> command-line parameter value is the |
| 129 |
location of the directory containing files to be indexed.</p> |
| 130 |
<p>The <span class="codefrag">-update</span> command-line parameter tells |
| 131 |
<span class="codefrag">IndexFiles</span> not to delete the index if it already |
| 132 |
exists. When <span class="codefrag">-update</span> is not given, <span class= |
| 133 |
"codefrag">IndexFiles</span> will first wipe the slate clean before indexing |
| 134 |
any documents.</p> |
| 135 |
<p>Lucene {@link org.apache.lucene.store.Directory Directory}s are used by |
| 136 |
the <span class="codefrag">IndexWriter</span> to store information in the |
| 137 |
index. In addition to the {@link org.apache.lucene.store.FSDirectory FSDirectory} |
| 138 |
implementation we are using, there are several other <span class= |
| 139 |
"codefrag">Directory</span> subclasses that can write to RAM, to databases, |
| 140 |
etc.</p> |
| 141 |
<p>Lucene {@link org.apache.lucene.analysis.Analyzer Analyzer}s are |
| 142 |
processing pipelines that break up text into indexed tokens, a.k.a. terms, and |
| 143 |
optionally perform other operations on these tokens, e.g. downcasing, synonym |
| 144 |
insertion, filtering out unwanted tokens, etc. The <span class= |
| 145 |
"codefrag">Analyzer</span> we are using is <span class= |
| 146 |
"codefrag">StandardAnalyzer</span>, which creates tokens using the Word Break |
| 147 |
rules from the Unicode Text Segmentation algorithm specified in <a href= |
| 148 |
"http://unicode.org/reports/tr29/">Unicode Standard Annex #29</a>; converts |
| 149 |
tokens to lowercase; and then filters out stopwords. Stopwords are common |
| 150 |
language words such as articles (a, an, the, etc.) and other tokens that may |
| 151 |
have less value for searching. It should be noted that there are different |
| 152 |
rules for every language, and you should use the proper analyzer for each. |
| 153 |
Lucene currently provides Analyzers for a number of different languages (see |
| 154 |
the javadocs under <a href= |
| 155 |
"../analysis/common/overview-summary.html">lucene/analysis/common/src/java/org/apache/lucene/analysis</a>).</p> |
| 156 |
<p>The <span class="codefrag">IndexWriterConfig</span> instance holds all |
| 157 |
configuration for <span class="codefrag">IndexWriter</span>. For example, we |
| 158 |
set the <span class="codefrag">OpenMode</span> to use here based on the value |
| 159 |
of the <span class="codefrag">-update</span> command-line parameter.</p> |
| 160 |
<p>Looking further down in the file, after <span class= |
| 161 |
"codefrag">IndexWriter</span> is instantiated, you should see the <span class= |
| 162 |
"codefrag">indexDocs()</span> code. This recursive function crawls the |
| 163 |
directories and creates {@link org.apache.lucene.document.Document Document} objects. The |
| 164 |
<span class="codefrag">Document</span> is simply a data object to represent the |
| 165 |
text content from the file as well as its creation time and location. These |
| 166 |
instances are added to the <span class="codefrag">IndexWriter</span>. If the |
| 167 |
<span class="codefrag">-update</span> command-line parameter is given, the |
| 168 |
<span class="codefrag">IndexWriterConfig</span> <span class= |
| 169 |
"codefrag">OpenMode</span> will be set to {@link org.apache.lucene.index.IndexWriterConfig.OpenMode#CREATE_OR_APPEND |
| 170 |
OpenMode.CREATE_OR_APPEND}, and rather than adding documents |
| 171 |
to the index, the <span class="codefrag">IndexWriter</span> will |
| 172 |
<strong>update</strong> them in the index by attempting to find an |
| 173 |
already-indexed document with the same identifier (in our case, the file path |
| 174 |
serves as the identifier); deleting it from the index if it exists; and then |
| 175 |
adding the new document to the index.</p> |
| 176 |
</div> |
| 177 |
<a id="Searching_Files"></a> |
| 178 |
<h2 class="boxed">Searching Files</h2> |
| 179 |
<div class="section"> |
| 180 |
<p>The <a href= |
| 181 |
"src-html/org/apache/lucene/demo/SearchFiles.html">SearchFiles</a> class is |
| 182 |
quite simple. It primarily collaborates with an |
| 183 |
{@link org.apache.lucene.search.IndexSearcher IndexSearcher}, |
| 184 |
{@link org.apache.lucene.analysis.standard.StandardAnalyzer StandardAnalyzer}, |
| 185 |
(which is used in the <a href= |
| 186 |
"src-html/org/apache/lucene/demo/IndexFiles.html">IndexFiles</a> class as well) |
| 187 |
and a {@link org.apache.lucene.queryparser.classic.QueryParser QueryParser}. The |
| 188 |
query parser is constructed with an analyzer used to interpret your query text |
| 189 |
in the same way the documents are interpreted: finding word boundaries, |
| 190 |
downcasing, and removing useless words like 'a', 'an' and 'the'. The |
| 191 |
{@link org.apache.lucene.search.Query} object contains the |
| 192 |
results from the |
| 193 |
{@link org.apache.lucene.queryparser.classic.QueryParser QueryParser} which |
| 194 |
is passed to the searcher. Note that it's also possible to programmatically |
| 195 |
construct a rich {@link org.apache.lucene.search.Query} object without using |
| 196 |
the query parser. The query parser just enables decoding the <a href= |
| 197 |
"../queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package_description"> |
| 198 |
Lucene query syntax</a> into the corresponding |
| 199 |
{@link org.apache.lucene.search.Query Query} object.</p> |
| 200 |
<p><span class="codefrag">SearchFiles</span> uses the |
| 201 |
{@link org.apache.lucene.search.IndexSearcher#search(org.apache.lucene.search.Query,int) |
| 202 |
IndexSearcher.search(query,n)} method that returns |
| 203 |
{@link org.apache.lucene.search.TopDocs TopDocs} with max |
| 204 |
<span class="codefrag">n</span> hits. The results are printed in pages, sorted |
| 205 |
by score (i.e. relevance).</p> |
| 206 |
</div> |
| 207 |
<h2 id="Embeddings" class="boxed">Working with vector embeddings</h2> |
| 208 |
<div class="section"> |
| 209 |
<p>In addition to indexing and searching text, IndexFiles and SearchFiles can also index and search |
| 210 |
numeric vectors derived from that text, known as "embeddings." This demo code uses pre-computed embeddings |
| 211 |
provided by the <a href="https://nlp.stanford.edu/projects/glove/">GloVe</a> project, which are in the public |
| 212 |
domain. The dictionary here is a tiny subset of the full GloVe dataset. It includes only the words that occur |
| 213 |
in the toy data set, and is definitely <i>not ready for production use</i>! If you use this code to create |
| 214 |
a vector index for a larger document set, the indexer will throw an exception because |
| 215 |
a more complete set of embeddings is needed to get reasonable results. |
| 216 |
</p> |
| 217 |
</div> |
| 218 |
<h2 id="Faceting" class="boxed">Working with facets</h2> |
| 219 |
<div class="section"> |
| 220 |
<p>Lucene also provides aggregation capabilities over the index, e.g. counting results across a category |
| 221 |
(<a href="src-html/org/apache/lucene/demo/facet/SimpleFacetsExample.html">SimpleFacetsExample</a>), |
| 222 |
computing expressions (<a href= |
| 223 |
"src-html/org/apache/lucene/demo/facet/ExpressionAggregationFacetsExample.html"> |
| 224 |
ExpressionAggregationFacetsExample</a>), dynamic ranges (<a href= |
| 225 |
"src-html/org/apache/lucene/demo/facet/DynamicRangeFacetsExample.html">DynamicRangeFacetsExample</a>). |
| 226 |
For more details, see the dedicated |
| 227 |
<a href="org/apache/lucene/demo/facet/package-summary.html">faceting guide</a>. |
| 228 |
</p> |
| 229 |
</div> |
| 230 |
</body> |
| 231 |
</html> |