|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.lucene.index.IndexModifier
public class IndexModifier
A class to modify an index, i.e. to delete and add documents. This
class hides IndexReader
and 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();
|
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.
Field Summary | |
---|---|
protected Analyzer |
analyzer
|
protected Directory |
directory
|
protected IndexReader |
indexReader
|
protected IndexWriter |
indexWriter
|
protected java.io.PrintStream |
infoStream
|
protected int |
maxBufferedDocs
|
protected int |
maxFieldLength
|
protected int |
mergeFactor
|
protected boolean |
open
|
protected boolean |
useCompoundFile
|
Constructor Summary | |
---|---|
IndexModifier(Directory directory,
Analyzer analyzer,
boolean create)
Open an index with write access. |
|
IndexModifier(java.io.File file,
Analyzer analyzer,
boolean create)
Open an index with write access. |
|
IndexModifier(java.lang.String dirName,
Analyzer analyzer,
boolean create)
Open an index with write access. |
Method Summary | |
---|---|
void |
addDocument(Document doc)
Adds a document to this index. |
void |
addDocument(Document doc,
Analyzer docAnalyzer)
Adds a document to this index, using the provided analyzer instead of the one specific in the constructor. |
protected void |
assureOpen()
Throw an IllegalStateException if the index is closed. |
void |
close()
Close this index, writing all pending changes to disk. |
protected void |
createIndexReader()
Close the IndexWriter and open an IndexReader. |
protected void |
createIndexWriter()
Close the IndexReader and open an IndexWriter. |
void |
delete(int docNum)
Deprecated. Use deleteDocument(int) instead. |
int |
delete(Term term)
Deprecated. Use deleteDocuments(Term) instead. |
void |
deleteDocument(int docNum)
Deletes the document numbered docNum . |
int |
deleteDocuments(Term term)
Deletes all documents containing term . |
int |
docCount()
Returns the number of documents currently in this index. |
void |
flush()
Make sure all changes are written to disk. |
java.io.PrintStream |
getInfoStream()
|
int |
getMaxBufferedDocs()
|
int |
getMaxFieldLength()
|
int |
getMergeFactor()
|
boolean |
getUseCompoundFile()
|
protected void |
init(Directory directory,
Analyzer analyzer,
boolean create)
Initialize an IndexWriter. |
void |
optimize()
Merges all segments together into a single segment, optimizing an index for search. |
void |
setInfoStream(java.io.PrintStream infoStream)
If non-null, information about merges and a message when getMaxFieldLength() is reached will be printed to this. |
void |
setMaxBufferedDocs(int maxBufferedDocs)
The maximum number of terms that will be indexed for a single field in a document. |
void |
setMaxFieldLength(int maxFieldLength)
The maximum number of terms that will be indexed for a single field in a document. |
void |
setMergeFactor(int mergeFactor)
Determines how often segment indices are merged by addDocument(). |
void |
setUseCompoundFile(boolean useCompoundFile)
Setting to turn on usage of a compound file. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected IndexWriter indexWriter
protected IndexReader indexReader
protected Directory directory
protected Analyzer analyzer
protected boolean open
protected java.io.PrintStream infoStream
protected boolean useCompoundFile
protected int maxBufferedDocs
protected int maxFieldLength
protected int mergeFactor
Constructor Detail |
---|
public IndexModifier(Directory directory, Analyzer analyzer, boolean create) throws java.io.IOException
directory
- the index directoryanalyzer
- the analyzer to use for adding new documentscreate
- true
to create the index or overwrite the existing one;
false
to append to the existing index
java.io.IOException
public IndexModifier(java.lang.String dirName, Analyzer analyzer, boolean create) throws java.io.IOException
dirName
- the index directoryanalyzer
- the analyzer to use for adding new documentscreate
- true
to create the index or overwrite the existing one;
false
to append to the existing index
java.io.IOException
public IndexModifier(java.io.File file, Analyzer analyzer, boolean create) throws java.io.IOException
file
- the index directoryanalyzer
- the analyzer to use for adding new documentscreate
- true
to create the index or overwrite the existing one;
false
to append to the existing index
java.io.IOException
Method Detail |
---|
protected void init(Directory directory, Analyzer analyzer, boolean create) throws java.io.IOException
java.io.IOException
protected void assureOpen()
java.lang.IllegalStateException
protected void createIndexWriter() throws java.io.IOException
java.io.IOException
protected void createIndexReader() throws java.io.IOException
java.io.IOException
public void flush() throws java.io.IOException
java.io.IOException
public void addDocument(Document doc, Analyzer docAnalyzer) throws java.io.IOException
setMaxFieldLength(int)
terms for a given field, the remainder are
discarded.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexWriter.addDocument(Document, Analyzer)
public void addDocument(Document doc) throws java.io.IOException
setMaxFieldLength(int)
terms for a given field, the remainder are
discarded.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexWriter.addDocument(Document)
public int deleteDocuments(Term term) throws java.io.IOException
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.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexReader.deleteDocuments(Term)
public int delete(Term term) throws java.io.IOException
deleteDocuments(Term)
instead.
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.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexReader.deleteDocuments(Term)
public void deleteDocument(int docNum) throws java.io.IOException
docNum
.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexReader.deleteDocument(int)
public void delete(int docNum) throws java.io.IOException
deleteDocument(int)
instead.
docNum
.
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexReader.deleteDocument(int)
public int docCount()
java.lang.IllegalStateException
- if the index is closedIndexWriter.docCount()
,
IndexReader.numDocs()
public void optimize() throws java.io.IOException
java.lang.IllegalStateException
- if the index is closed
java.io.IOException
IndexWriter.optimize()
public void setInfoStream(java.io.PrintStream infoStream)
getMaxFieldLength()
is reached will be printed to this.
Example: index.setInfoStream(System.err);
java.lang.IllegalStateException
- if the index is closedIndexWriter.setInfoStream(PrintStream)
public java.io.PrintStream getInfoStream() throws java.io.IOException
java.io.IOException
setInfoStream(PrintStream)
public void setUseCompoundFile(boolean useCompoundFile)
java.lang.IllegalStateException
- if the index is closedIndexWriter.setUseCompoundFile(boolean)
public boolean getUseCompoundFile() throws java.io.IOException
java.io.IOException
setUseCompoundFile(boolean)
public void setMaxFieldLength(int maxFieldLength)
java.lang.IllegalStateException
- if the index is closedIndexWriter.setMaxFieldLength(int)
public int getMaxFieldLength() throws java.io.IOException
java.io.IOException
setMaxFieldLength(int)
public void setMaxBufferedDocs(int maxBufferedDocs)
java.lang.IllegalStateException
- if the index is closedIndexWriter.setMaxBufferedDocs(int)
public int getMaxBufferedDocs() throws java.io.IOException
java.io.IOException
setMaxBufferedDocs(int)
public void setMergeFactor(int mergeFactor)
This must never be less than 2. The default value is 10.
java.lang.IllegalStateException
- if the index is closedIndexWriter.setMergeFactor(int)
public int getMergeFactor() throws java.io.IOException
java.io.IOException
setMergeFactor(int)
public void close() throws java.io.IOException
java.lang.IllegalStateException
- if the index has been closed before already
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |