org.apache.lucene.index

Class IndexWriter

public class IndexWriter extends Object

An IndexWriter creates and maintains an index. The third argument to the constructor determines whether a new index is created, or whether an existing index is opened for the addition of new documents. In either case, documents are added with the addDocument method. When finished adding documents, close should be called. 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.
Field Summary
static StringCOMMIT_LOCK_NAME
static longCOMMIT_LOCK_TIMEOUT
Default value is 10000.
static intDEFAULT_MAX_FIELD_LENGTH
Default value is 10000.
static intDEFAULT_MAX_MERGE_DOCS
Default value is {@link Integer#MAX_VALUE}.
static intDEFAULT_MERGE_FACTOR
Default value is 10.
static intDEFAULT_MIN_MERGE_DOCS
Default value is 10.
PrintStreaminfoStream
If non-null, information about merges will be printed to this.
intmaxFieldLength
The maximum number of terms that will be indexed for a single field in a document.
intmaxMergeDocs
Determines the largest number of documents ever merged by addDocument().
intmergeFactor
Determines how often segment indices are merged by addDocument().
intminMergeDocs
Determines the minimal number of documents required before the buffered in-memory documents are merging and a new Segment is created.
static StringWRITE_LOCK_NAME
static longWRITE_LOCK_TIMEOUT
Default value is 1000.
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
voidaddDocument(Document doc)
Adds a document to this index.
voidaddDocument(Document doc, Analyzer analyzer)
Adds a document to this index, using the provided analyzer instead of the value of {@link #getAnalyzer()}.
voidaddIndexes(Directory[] dirs)
Merges all segments from an array of indexes into this index.
voidaddIndexes(IndexReader[] readers)
Merges the provided indexes into this index.
voidclose()
Flushes all changes to an index and closes all associated files.
intdocCount()
Returns the number of documents currently in this index.
protected voidfinalize()
Release the write lock, if needed.
AnalyzergetAnalyzer()
Returns the analyzer used by this index.
SimilaritygetSimilarity()
Expert: Return the Similarity implementation used by this IndexWriter.
booleangetUseCompoundFile()
Setting to turn on usage of a compound file.
voidoptimize()
Merges all segments together into a single segment, optimizing an index for search.
voidsetSimilarity(Similarity similarity)
Expert: Set the Similarity implementation used by this IndexWriter.
voidsetUseCompoundFile(boolean value)
Setting to turn on usage of a compound file.

Field Detail

COMMIT_LOCK_NAME

public static final String COMMIT_LOCK_NAME

COMMIT_LOCK_TIMEOUT

public static long COMMIT_LOCK_TIMEOUT
Default value is 10000. Use org.apache.lucene.commitLockTimeout system property to override.

DEFAULT_MAX_FIELD_LENGTH

public static final int DEFAULT_MAX_FIELD_LENGTH
Default value is 10000. Use org.apache.lucene.maxFieldLength system property to override.

DEFAULT_MAX_MERGE_DOCS

public static final int DEFAULT_MAX_MERGE_DOCS
Default value is {@link Integer#MAX_VALUE}. Use org.apache.lucene.maxMergeDocs system property to override.

DEFAULT_MERGE_FACTOR

public static final int DEFAULT_MERGE_FACTOR
Default value is 10. Use org.apache.lucene.mergeFactor system property to override.

DEFAULT_MIN_MERGE_DOCS

public static final int DEFAULT_MIN_MERGE_DOCS
Default value is 10. Use org.apache.lucene.minMergeDocs system property to override.

infoStream

public PrintStream infoStream
If non-null, information about merges will be printed to this.

maxFieldLength

public int maxFieldLength
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.

maxMergeDocs

public int maxMergeDocs
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 {@link Integer#MAX_VALUE}.

mergeFactor

public int mergeFactor
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.

minMergeDocs

public int minMergeDocs
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 {@link org.apache.lucene.store.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.

WRITE_LOCK_NAME

public static final String WRITE_LOCK_NAME

WRITE_LOCK_TIMEOUT

public static long WRITE_LOCK_TIMEOUT
Default value is 1000. Use org.apache.lucene.writeLockTimeout system property to override.

Constructor Detail

IndexWriter

public IndexWriter(String path, Analyzer a, boolean create)
Constructs an IndexWriter for the index in 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

IndexWriter

public IndexWriter(File path, Analyzer a, boolean create)
Constructs an IndexWriter for the index in 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

IndexWriter

public IndexWriter(Directory d, Analyzer a, boolean create)
Constructs an IndexWriter for the index in 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

Method Detail

addDocument

public void addDocument(Document doc)
Adds a document to this index. If the document contains more than {@link #maxFieldLength} terms for a given field, the remainder are discarded.

addDocument

public void addDocument(Document doc, Analyzer analyzer)
Adds a document to this index, using the provided analyzer instead of the value of {@link #getAnalyzer()}. If the document contains more than {@link #maxFieldLength} terms for a given field, the remainder are discarded.

addIndexes

public void addIndexes(Directory[] dirs)
Merges all segments from an array of indexes into this index.

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.

addIndexes

public void addIndexes(IndexReader[] readers)
Merges the provided indexes into this index.

After this completes, the index is optimized.

The provided IndexReaders are not closed.

close

public void close()
Flushes all changes to an index and closes all associated files.

docCount

public int docCount()
Returns the number of documents currently in this index.

finalize

protected void finalize()
Release the write lock, if needed.

getAnalyzer

public Analyzer getAnalyzer()
Returns the analyzer used by this index.

getSimilarity

public Similarity getSimilarity()
Expert: Return the Similarity implementation used by this IndexWriter.

This defaults to the current value of {@link Similarity#getDefault()}.

getUseCompoundFile

public boolean getUseCompoundFile()
Setting to turn on usage of a compound file. When on, multiple files for each segment are merged into a single file once the segment creation is finished. This is done regardless of what directory is in use.

optimize

public void optimize()
Merges all segments together into a single segment, optimizing an index for search.

setSimilarity

public void setSimilarity(Similarity similarity)
Expert: Set the Similarity implementation used by this IndexWriter.

See Also: setDefault

setUseCompoundFile

public void setUseCompoundFile(boolean value)
Setting to turn on usage of a compound file. When on, multiple files for each segment are merged into a single file once the segment creation is finished. This is done regardless of what directory is in use.
Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.