org.apache.lucene.index
public class IndexWriter extends Object
If an index will not have more documents added for a while and optimal search performance is desired, then the optimize method should be called before the index is closed.
Opening an IndexWriter creates a lock file for the directory in use. Trying to open another IndexWriter on the same directory will lead to an IOException. The IOException is also thrown if an IndexReader on the same directory is used to delete documents from the index.
See Also: IndexModifier supports the important methods of IndexWriter plus deletion
Field Summary | |
---|---|
static String | COMMIT_LOCK_NAME |
static long | COMMIT_LOCK_TIMEOUT
Default value is 10,000. |
static int | DEFAULT_MAX_BUFFERED_DOCS
Default value is 10. |
static int | DEFAULT_MAX_FIELD_LENGTH
Default value is 10,000. |
static int | DEFAULT_MAX_MERGE_DOCS
Default value is Integer#MAX_VALUE. |
static int | DEFAULT_MERGE_FACTOR
Default value is 10. |
static int | DEFAULT_MIN_MERGE_DOCS |
static int | DEFAULT_TERM_INDEX_INTERVAL
Default value is 128. |
PrintStream | infoStream If non-null, information about merges will be printed to this. |
int | maxFieldLength
The maximum number of terms that will be indexed for a single field in a
document. |
int | maxMergeDocs Determines the largest number of documents ever merged by addDocument().
|
int | mergeFactor Determines how often segment indices are merged by addDocument(). |
int | minMergeDocs Determines the minimal number of documents required before the buffered
in-memory documents are merging and a new Segment is created.
|
static String | WRITE_LOCK_NAME |
static long | WRITE_LOCK_TIMEOUT
Default value is 1,000. |
Constructor Summary | |
---|---|
IndexWriter(String path, Analyzer a, boolean create)
Constructs an IndexWriter for the index in path .
| |
IndexWriter(File path, Analyzer a, boolean create)
Constructs an IndexWriter for the index in path .
| |
IndexWriter(Directory d, Analyzer a, boolean create)
Constructs an IndexWriter for the index in d .
|
Method Summary | |
---|---|
void | addDocument(Document doc)
Adds a document to this index. |
void | addDocument(Document doc, Analyzer analyzer)
Adds a document to this index, using the provided analyzer instead of the
value of getAnalyzer. |
void | addIndexes(Directory[] dirs) Merges all segments from an array of indexes into this index.
|
void | addIndexes(IndexReader[] readers) Merges the provided indexes into this index.
|
void | close() Flushes all changes to an index and closes all associated files. |
int | docCount() Returns the number of documents currently in this index. |
protected void | finalize() Release the write lock, if needed. |
Analyzer | getAnalyzer() Returns the analyzer used by this index. |
Directory | getDirectory() Returns the Directory used by this index. |
PrintStream | getInfoStream() |
int | getMaxBufferedDocs() |
int | getMaxFieldLength() |
int | getMaxMergeDocs() |
int | getMergeFactor() |
Similarity | getSimilarity() Expert: Return the Similarity implementation used by this IndexWriter.
|
int | getTermIndexInterval() Expert: Return the interval between indexed terms.
|
boolean | getUseCompoundFile() Get the current setting of whether to use the compound file format.
|
void | optimize() Merges all segments together into a single segment, optimizing an index
for search. |
void | setInfoStream(PrintStream infoStream) If non-null, information about merges and a message when
maxFieldLength is reached will be printed to this. |
void | setMaxBufferedDocs(int maxBufferedDocs) Determines the minimal number of documents required before the buffered
in-memory documents are merging and a new Segment is created.
|
void | setMaxFieldLength(int maxFieldLength)
The maximum number of terms that will be indexed for a single field in a
document. |
void | setMaxMergeDocs(int maxMergeDocs) Determines the largest number of documents ever merged by addDocument().
|
void | setMergeFactor(int mergeFactor) Determines how often segment indices are merged by addDocument(). |
void | setSimilarity(Similarity similarity) Expert: Set the Similarity implementation used by this IndexWriter.
|
void | setTermIndexInterval(int interval) Expert: Set the interval between indexed terms. |
void | setUseCompoundFile(boolean value) Setting to turn on usage of a compound file. |
Deprecated: use DEFAULT_MAX_BUFFERED_DOCS instead
Deprecated: use IndexWriter instead
If non-null, information about merges will be printed to this.Deprecated: use IndexWriter instead
The maximum number of terms that will be indexed for a single field in a document. This limits the amount of memory required for indexing, so that collections with very large files will not crash the indexing process by running out of memory. Note that this effectively truncates large documents, excluding from the index terms that occur further in the document. If you know your source documents are large, be sure to set this value high enough to accomodate the expected size. If you set it to Integer.MAX_VALUE, then the only limit is your memory, but you should anticipate an OutOfMemoryError. By default, no more than 10,000 terms will be indexed for a field.Deprecated: use IndexWriter instead
Determines the largest number of documents ever merged by addDocument(). Small values (e.g., less than 10,000) are best for interactive indexing, as this limits the length of pauses while indexing to a few seconds. Larger values are best for batched indexing and speedier searches.The default value is Integer#MAX_VALUE.
Deprecated: use IndexWriter instead
Determines how often segment indices are merged by addDocument(). With smaller values, less RAM is used while indexing, and searches on unoptimized indices are faster, but indexing speed is slower. With larger values, more RAM is used during indexing, and while searches on unoptimized indices are slower, indexing is faster. Thus larger values (> 10) are best for batch index creation, and smaller values (< 10) for indices that are interactively maintained.This must never be less than 2. The default value is 10.
Deprecated: use IndexWriter instead
Determines the minimal number of documents required before the buffered in-memory documents are merging and a new Segment is created. Since Documents are merged in a RAMDirectory, large value gives faster indexing. At the same time, mergeFactor limits the number of files open in a FSDirectory.The default value is 10.
path
.
Text will be analyzed with a
. If create
is true, then a new, empty index will be created in
path
, replacing the index already there, if any.
Parameters: path the path to the index directory a the analyzer to use create true
to create the index or overwrite
the existing one; false
to append to the existing
index
Throws: IOException if the directory cannot be read/written to, or
if it does not exist, and create
is
false
path
.
Text will be analyzed with a
. If create
is true, then a new, empty index will be created in
path
, replacing the index already there, if any.
Parameters: path the path to the index directory a the analyzer to use create true
to create the index or overwrite
the existing one; false
to append to the existing
index
Throws: IOException if the directory cannot be read/written to, or
if it does not exist, and create
is
false
d
.
Text will be analyzed with a
. If create
is true, then a new, empty index will be created in
d
, replacing the index already there, if any.
Parameters: d the index directory a the analyzer to use create true
to create the index or overwrite
the existing one; false
to append to the existing
index
Throws: IOException if the directory cannot be read/written to, or
if it does not exist, and create
is
false
This may be used to parallelize batch indexing. A large document collection can be broken into sub-collections. Each sub-collection can be indexed in parallel, on a different thread, process or machine. The complete index can then be created by merging sub-collection indexes with this method.
After this completes, the index is optimized.
After this completes, the index is optimized.
The provided IndexReaders are not closed.
See Also: IndexWriter
See Also: IndexWriter
See Also: IndexWriter
See Also: IndexWriter
See Also: IndexWriter
This defaults to the current value of getDefault.
See Also: IndexWriter
See Also: IndexWriter
The default value is 10.
Throws: IllegalArgumentException if maxBufferedDocs is smaller than 2
The default value is Integer#MAX_VALUE.
This must never be less than 2. The default value is 10.
See Also: setDefault
numUniqueTerms/interval
terms are read into
memory by an IndexReader, and, on average, interval/2
terms
must be scanned for each random term access.
See Also: DEFAULT_TERM_INDEX_INTERVAL