-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
overview.html
176 lines (153 loc) · 8.29 KB
/
overview.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>Apache Lucene API</title>
</head>
<body>
<p>Apache Lucene is a high-performance, full-featured text search engine library.
Here's a simple example how to use Lucene for indexing and searching (using JUnit
to check if the results are what we expect):</p>
<!-- code comes from org.apache.lucene.TestDemo: -->
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- = Java2Html Converter 5.0 [2006-03-04] by Markus Gebhard [email protected] = -->
<!-- = Further information: http://www.java2html.de = -->
<pre class="prettyprint">
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
// Store the index in memory:
Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
//Directory directory = FSDirectory.open("/tmp/testindex");
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);
Document doc = new Document();
String text = "This is the text to be indexed.";
doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close();
// Now search the index:
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
// Parse a simple query that searches for "text":
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
Query query = parser.parse("text");
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
assertEquals(1, hits.length);
// Iterate through the results:
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
}
ireader.close();
directory.close();</pre>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== -->
<p>The Lucene API is divided into several packages:</p>
<ul>
<li>
<b>{@link org.apache.lucene.analysis}</b>
defines an abstract {@link org.apache.lucene.analysis.Analyzer Analyzer}
API for converting text from a {@link java.io.Reader}
into a {@link org.apache.lucene.analysis.TokenStream TokenStream},
an enumeration of token {@link org.apache.lucene.util.Attribute Attribute}s.
A TokenStream can be composed by applying {@link org.apache.lucene.analysis.TokenFilter TokenFilter}s
to the output of a {@link org.apache.lucene.analysis.Tokenizer Tokenizer}.
Tokenizers and TokenFilters are strung together and applied with an {@link org.apache.lucene.analysis.Analyzer Analyzer}.
<a href="../analyzers-common/overview-summary.html">analyzers-common</a> provides a number of Analyzer implementations, including
<a href="../analyzers-common/org/apache/lucene/analysis/core/StopAnalyzer.html">StopAnalyzer</a>
and the grammar-based <a href="../analyzers-common/org/apache/lucene/analysis/standard/StandardAnalyzer.html">StandardAnalyzer</a>.</li>
<li>
<b>{@link org.apache.lucene.codecs}</b>
provides an abstraction over the encoding and decoding of the inverted index structure,
as well as different implementations that can be chosen depending upon application needs.
<li>
<b>{@link org.apache.lucene.document}</b>
provides a simple {@link org.apache.lucene.document.Document Document}
class. A Document is simply a set of named {@link org.apache.lucene.document.Field Field}s,
whose values may be strings or instances of {@link java.io.Reader}.</li>
<li>
<b>{@link org.apache.lucene.index}</b>
provides two primary classes: {@link org.apache.lucene.index.IndexWriter IndexWriter},
which creates and adds documents to indices; and {@link org.apache.lucene.index.IndexReader},
which accesses the data in the index.</li>
<li>
<b>{@link org.apache.lucene.search}</b>
provides data structures to represent queries (ie {@link org.apache.lucene.search.TermQuery TermQuery}
for individual words, {@link org.apache.lucene.search.PhraseQuery PhraseQuery}
for phrases, and {@link org.apache.lucene.search.BooleanQuery BooleanQuery}
for boolean combinations of queries) and the {@link org.apache.lucene.search.IndexSearcher IndexSearcher}
which turns queries into {@link org.apache.lucene.search.TopDocs TopDocs}.
A number of <a href="../queryparser/overview-summary.html">QueryParser</a>s are provided for producing
query structures from strings or xml.
<li>
<b>{@link org.apache.lucene.store}</b>
defines an abstract class for storing persistent data, the {@link org.apache.lucene.store.Directory Directory},
which is a collection of named files written by an {@link org.apache.lucene.store.IndexOutput IndexOutput}
and read by an {@link org.apache.lucene.store.IndexInput IndexInput}.
Multiple implementations are provided, including {@link org.apache.lucene.store.FSDirectory FSDirectory},
which uses a file system directory to store files, and {@link org.apache.lucene.store.RAMDirectory RAMDirectory}
which implements files as memory-resident data structures.</li>
<li>
<b>{@link org.apache.lucene.util}</b>
contains a few handy data structures and util classes, ie {@link org.apache.lucene.util.OpenBitSet OpenBitSet}
and {@link org.apache.lucene.util.PriorityQueue PriorityQueue}.</li>
</ul>
To use Lucene, an application should:
<ol>
<li>
Create {@link org.apache.lucene.document.Document Document}s by
adding
{@link org.apache.lucene.document.Field Field}s;</li>
<li>
Create an {@link org.apache.lucene.index.IndexWriter IndexWriter}
and add documents to it with {@link org.apache.lucene.index.IndexWriter#addDocument(Iterable) addDocument()};</li>
<li>
Call <a href="../queryparser/org/apache/lucene/queryparser/classic/QueryParserBase.html#parse(java.lang.String)">QueryParser.parse()</a>
to build a query from a string; and</li>
<li>
Create an {@link org.apache.lucene.search.IndexSearcher IndexSearcher}
and pass the query to its {@link org.apache.lucene.search.IndexSearcher#search(org.apache.lucene.search.Query, int) search()}
method.</li>
</ol>
Some simple examples of code which does this are:
<ul>
<li>
<a href="../demo/src-html/org/apache/lucene/demo/IndexFiles.html">IndexFiles.java</a> creates an
index for all the files contained in a directory.</li>
<li>
<a href="../demo/src-html/org/apache/lucene/demo/SearchFiles.html">SearchFiles.java</a> prompts for
queries and searches an index.</li>
</ul>
To demonstrate these, try something like:
<blockquote><tt>> <b>java -cp lucene-core.jar:lucene-demo.jar:lucene-analyzers-common.jar org.apache.lucene.demo.IndexFiles -index index -docs rec.food.recipes/soups</b></tt>
<br><tt>adding rec.food.recipes/soups/abalone-chowder</tt>
<br><tt> </tt>[ ... ]
<p><tt>> <b>java -cp lucene-core.jar:lucene-demo.jar:lucene-queryparser.jar:lucene-analyzers-common.jar org.apache.lucene.demo.SearchFiles</b></tt>
<br><tt>Query: <b>chowder</b></tt>
<br><tt>Searching for: chowder</tt>
<br><tt>34 total matching documents</tt>
<br><tt>1. rec.food.recipes/soups/spam-chowder</tt>
<br><tt> </tt>[ ... thirty-four documents contain the word "chowder" ... ]
<p><tt>Query: <b>"clam chowder" AND Manhattan</b></tt>
<br><tt>Searching for: +"clam chowder" +manhattan</tt>
<br><tt>2 total matching documents</tt>
<br><tt>1. rec.food.recipes/soups/clam-chowder</tt>
<br><tt> </tt>[ ... two documents contain the phrase "clam chowder"
and the word "manhattan" ... ]
<br> [ Note: "+" and "-" are canonical, but "AND", "OR"
and "NOT" may be used. ]</blockquote>
</body>
</html>