org.apache.lucene.index

Class IndexModifier

public class IndexModifier extends Object

A class to modify an index, i.e. to delete and add documents. This class hides {@link IndexReader} and {@link IndexWriter} so that you do not need to care about implementation details such as that adding documents is done via IndexWriter and deletion is done via IndexReader.

Note that you cannot create more than one IndexModifier object on the same directory at the same time.

Example usage:

    Analyzer analyzer = new StandardAnalyzer();
    // create an index in /tmp/index, overwriting an existing one:
    IndexModifier indexModifier = new IndexModifier("/tmp/index", analyzer, true);
    Document doc = new Document();
    doc.add(new Field("id""1", Field.Store.YES, Field.Index.UN_TOKENIZED));
    doc.add(new Field("body""a simple test", Field.Store.YES, Field.Index.TOKENIZED));
    indexModifier.addDocument(doc);
    int deleted = indexModifier.delete(new Term("id""1"));
    System.out.println("Deleted " + deleted + " document");
    indexModifier.flush();
    System.out.println(indexModifier.docCount() " docs in index");
    indexModifier.close();

Not all methods of IndexReader and IndexWriter are offered by this class. If you need access to additional methods, either use those classes directly or implement your own class that extends IndexModifier.

Although an instance of this class can be used from more than one thread, you will not get the best performance. You might want to use IndexReader and IndexWriter directly for that (but you will need to care about synchronization yourself then).

While you can freely mix calls to add() and delete() using this class, you should batch you calls for best performance. For example, if you want to update 20 documents, you should first delete all those documents, then add all the new documents.

Author: Daniel Naber

Field Summary
protected Analyzeranalyzer
protected Directorydirectory
protected IndexReaderindexReader
protected IndexWriterindexWriter
protected PrintStreaminfoStream
protected intmaxBufferedDocs
protected intmaxFieldLength
protected intmergeFactor
protected booleanopen
protected booleanuseCompoundFile
Constructor Summary
IndexModifier(Directory directory, Analyzer analyzer, boolean create)
Open an index with write access.
IndexModifier(String dirName, Analyzer analyzer, boolean create)
Open an index with write access.
IndexModifier(File file, Analyzer analyzer, boolean create)
Open an index with write access.
Method Summary
voidaddDocument(Document doc, Analyzer docAnalyzer)
Adds a document to this index, using the provided analyzer instead of the one specific in the constructor.
voidaddDocument(Document doc)
Adds a document to this index.
protected voidassureOpen()
Throw an IllegalStateException if the index is closed.
voidclose()
Close this index, writing all pending changes to disk.
protected voidcreateIndexReader()
Close the IndexWriter and open an IndexReader.
protected voidcreateIndexWriter()
Close the IndexReader and open an IndexWriter.
intdelete(Term term)
Deletes all documents containing term.
voiddelete(int docNum)
Deletes the document numbered docNum.
voiddeleteDocument(int docNum)
Deletes the document numbered docNum.
intdeleteDocuments(Term term)
Deletes all documents containing term.
intdocCount()
Returns the number of documents currently in this index.
voidflush()
Make sure all changes are written to disk.
PrintStreamgetInfoStream()
intgetMaxBufferedDocs()
intgetMaxFieldLength()
intgetMergeFactor()
booleangetUseCompoundFile()
protected voidinit(Directory directory, Analyzer analyzer, boolean create)
Initialize an IndexWriter.
voidoptimize()
Merges all segments together into a single segment, optimizing an index for search.
voidsetInfoStream(PrintStream infoStream)
If non-null, information about merges and a message when {@link #getMaxFieldLength()} is reached will be printed to this.
voidsetMaxBufferedDocs(int maxBufferedDocs)
The maximum number of terms that will be indexed for a single field in a document.
voidsetMaxFieldLength(int maxFieldLength)
The maximum number of terms that will be indexed for a single field in a document.
voidsetMergeFactor(int mergeFactor)
Determines how often segment indices are merged by addDocument().
voidsetUseCompoundFile(boolean useCompoundFile)
Setting to turn on usage of a compound file.
StringtoString()

Field Detail

analyzer

protected Analyzer analyzer

directory

protected Directory directory

indexReader

protected IndexReader indexReader

indexWriter

protected IndexWriter indexWriter

infoStream

protected PrintStream infoStream

maxBufferedDocs

protected int maxBufferedDocs

maxFieldLength

protected int maxFieldLength

mergeFactor

protected int mergeFactor

open

protected boolean open

useCompoundFile

protected boolean useCompoundFile

Constructor Detail

IndexModifier

public IndexModifier(Directory directory, Analyzer analyzer, boolean create)
Open an index with write access.

Parameters: directory the index directory analyzer the analyzer to use for adding new documents create true to create the index or overwrite the existing one; false to append to the existing index

IndexModifier

public IndexModifier(String dirName, Analyzer analyzer, boolean create)
Open an index with write access.

Parameters: dirName the index directory analyzer the analyzer to use for adding new documents create true to create the index or overwrite the existing one; false to append to the existing index

IndexModifier

public IndexModifier(File file, Analyzer analyzer, boolean create)
Open an index with write access.

Parameters: file the index directory analyzer the analyzer to use for adding new documents create true to create the index or overwrite the existing one; false to append to the existing index

Method Detail

addDocument

public void addDocument(Document doc, Analyzer docAnalyzer)
Adds a document to this index, using the provided analyzer instead of the one specific in the constructor. If the document contains more than {@link #setMaxFieldLength(int)} terms for a given field, the remainder are discarded.

Throws: IllegalStateException if the index is closed

See Also: IndexWriter

addDocument

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

Throws: IllegalStateException if the index is closed

See Also: addDocument

assureOpen

protected void assureOpen()
Throw an IllegalStateException if the index is closed.

Throws: IllegalStateException

close

public void close()
Close this index, writing all pending changes to disk.

Throws: IllegalStateException if the index has been closed before already

createIndexReader

protected void createIndexReader()
Close the IndexWriter and open an IndexReader.

Throws: IOException

createIndexWriter

protected void createIndexWriter()
Close the IndexReader and open an IndexWriter.

Throws: IOException

delete

public int delete(Term term)

Deprecated: Use {@link #deleteDocuments(Term)} instead.

Deletes all documents containing term. This is useful if one uses a document field to hold a unique ID string for the document. Then to delete such a document, one merely constructs a term with the appropriate field and the unique ID string as its text and passes it to this method. Returns the number of documents deleted.

Returns: the number of documents deleted

Throws: IllegalStateException if the index is closed

See Also: deleteDocuments

delete

public void delete(int docNum)

Deprecated: Use {@link #deleteDocument(int)} instead.

Deletes the document numbered docNum.

Throws: IllegalStateException if the index is closed

See Also: IndexReader

deleteDocument

public void deleteDocument(int docNum)
Deletes the document numbered docNum.

Throws: IllegalStateException if the index is closed

See Also: IndexReader

deleteDocuments

public int deleteDocuments(Term term)
Deletes all documents containing term. This is useful if one uses a document field to hold a unique ID string for the document. Then to delete such a document, one merely constructs a term with the appropriate field and the unique ID string as its text and passes it to this method. Returns the number of documents deleted.

Returns: the number of documents deleted

Throws: IllegalStateException if the index is closed

See Also: deleteDocuments

docCount

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

Throws: IllegalStateException if the index is closed

See Also: docCount numDocs

flush

public void flush()
Make sure all changes are written to disk.

Throws: IOException

getInfoStream

public PrintStream getInfoStream()

Throws: IOException

See Also: setInfoStream

getMaxBufferedDocs

public int getMaxBufferedDocs()

Throws: IOException

See Also: IndexModifier

getMaxFieldLength

public int getMaxFieldLength()

Throws: IOException

See Also: IndexModifier

getMergeFactor

public int getMergeFactor()

Throws: IOException

See Also: IndexModifier

getUseCompoundFile

public boolean getUseCompoundFile()

Throws: IOException

See Also: IndexModifier

init

protected void init(Directory directory, Analyzer analyzer, boolean create)
Initialize an IndexWriter.

Throws: IOException

optimize

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

Throws: IllegalStateException if the index is closed

See Also: optimize

setInfoStream

public void setInfoStream(PrintStream infoStream)
If non-null, information about merges and a message when {@link #getMaxFieldLength()} is reached will be printed to this.

Example: index.setInfoStream(System.err);

Throws: IllegalStateException if the index is closed

See Also: setInfoStream

setMaxBufferedDocs

public void setMaxBufferedDocs(int maxBufferedDocs)
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.

Throws: IllegalStateException if the index is closed

See Also: IndexWriter

setMaxFieldLength

public void setMaxFieldLength(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.

Throws: IllegalStateException if the index is closed

See Also: IndexWriter

setMergeFactor

public void setMergeFactor(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.

Throws: IllegalStateException if the index is closed

See Also: IndexWriter

setUseCompoundFile

public void setUseCompoundFile(boolean useCompoundFile)
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.

Throws: IllegalStateException if the index is closed

See Also: IndexWriter

toString

public String toString()
Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.