Lucene - IndexWriter

This class acts as a core component which creates/updates indexes during indexing process.
Following is the declaration for org.apache.lucene.index.IndexWriter class −
public class IndexWriter
   extends Object
      implements Closeable, TwoPhaseCommit

Following are the fields for the org.apache.lucene.index.IndexWriter class −
static int DEFAULT_MAX_BUFFERED_DELETE_TERMS − Deprecated. use IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS instead.
static int DEFAULT_MAX_BUFFERED_DELETE_TERMS − Deprecated. use IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS instead.
static int DEFAULT_MAX_BUFFERED_DOCS − Deprecated. Use IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS instead.
static int DEFAULT_MAX_BUFFERED_DOCS − Deprecated. Use IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS instead.
static int DEFAULT_MAX_FIELD_LENGTH − Deprecated. See IndexWriterConfig.
static int DEFAULT_MAX_FIELD_LENGTH − Deprecated. See IndexWriterConfig.
static double DEFAULT_RAM_BUFFER_SIZE_MB − Deprecated. Use IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB instead.
static double DEFAULT_RAM_BUFFER_SIZE_MB − Deprecated. Use IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB instead.
static int DEFAULT_TERM_INDEX_INTERVAL − Deprecated. Use IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL instead.
static int DEFAULT_TERM_INDEX_INTERVAL − Deprecated. Use IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL instead.
static int DISABLE_AUTO_FLUSH − Deprecated. Use IndexWriterConfig.DISABLE_AUTO_FLUSH instead.
static int DISABLE_AUTO_FLUSH − Deprecated. Use IndexWriterConfig.DISABLE_AUTO_FLUSH instead.
static int MAX_TERM_LENGTH − Absolute maximum length for a term.
static int MAX_TERM_LENGTH − Absolute maximum length for a term.
static String WRITE_LOCK_NAME − Name of the write lock in the index.
static String WRITE_LOCK_NAME − Name of the write lock in the index.
static long WRITE_LOCK_TIMEOUT − Deprecated. Use IndexWriterConfig.WRITE_LOCK_TIMEOUT instead.
static long WRITE_LOCK_TIMEOUT − Deprecated. Use IndexWriterConfig.WRITE_LOCK_TIMEOUT instead.
Following table shows the class constructors for IndexWriter −
IndexWriter(Directory d, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy, IndexWriter.MaxFieldLength mfl)
Deprecated. Use IndexWriter(Directory, IndexWriterConfig) instead.
IndexWriter(Directory d, Analyzer a, boolean create, IndexWriter.MaxFieldLength mfl)
Deprecated. Use IndexWriter(Directory, IndexWriterConfig) instead.
IndexWriter(Directory d, Analyzer a, IndexDeletionPolicy deletionPolicy, IndexWriter.MaxFieldLength mfl)
Deprecated. Use IndexWriter(Directory, IndexWriterConfig) instead.
IndexWriter(Directory d, Analyzer a, IndexDeletionPolicy deletionPolicy, IndexWriter.MaxFieldLength mfl, IndexCommit commit)
Deprecated. Use IndexWriter(Directory, IndexWriterConfig) instead.
IndexWriter(Directory d, Analyzer a, IndexWriter.MaxFieldLength mfl)
Deprecated. Use IndexWriter(Directory, IndexWriterConfig) instead.
IndexWriter(Directory d, IndexWriterConfig conf)
Constructs a new IndexWriter per the settings given in conf.
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 addDocuments(Collection docs)
Atomically adds a block of documents with sequentially-assigned document IDs, such that an external reader will see all or none of the documents.
void addDocuments(Collection docs, Analyzer analyzer)
Atomically adds a block of documents, analyzed using the provided analyzer, with sequentially assigned document IDs, such that an external reader will see all or none of the documents.
void addIndexes(Directory... dirs)
Adds all segments from an array of indexes into this index.
void addIndexes(IndexReader... readers)
Merges the provided indexes into this index.
void addIndexesNoOptimize(Directory... dirs)
Deprecated. Use addIndexes(Directory...) instead.
void close()
Commits all changes to an index and closes all associated files.
void close(boolean waitForMerges)
Closes the index with or without waiting for currently running merges to finish.
void commit()
Commits all pending changes (added & deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.
void commit(Map<String,String> commitUserData)
Commits all changes to the index, specifying a commitUserData Map (String > String).
void deleteAll()
Deletes all documents in the index.
void deleteDocuments(Query... queries)
Deletes the document(s) matching any of the provided queries.
void deleteDocuments(Query query)
Deletes the document(s) matching the provided query.
void deleteDocuments(Term... terms)
Deletes the document(s) containing any of the terms.
void deleteDocuments(Term term)
Deletes the document(s) containing term.
void deleteUnusedFiles()
Expert: remove the index files that are no longer used.
protected void doAfterFlush()
A hook for extending classes to execute operations after pending added and deleted documents have been flushed to the Directory but before the change is committed (new segments_N file written).
protected void doBeforeFlush()
A hook for extending classes to execute operations before pending added and deleted documents are flushed to the Directory.
protected void ensureOpen()
protected void ensureOpen(boolean includePendingClose)
Used internally to throw an AlreadyClosedException if this IndexWriter has been closed.
void expungeDeletes()
Deprecated.
void expungeDeletes(boolean doWait)
Deprecated.
protected void flush(boolean triggerMerge, boolean applyAllDeletes)
Flushes all in-memory buffered updates (adds and deletes) to the Directory.
protected void flush(boolean triggerMerge, boolean flushDocStores, boolean flushDeletes)
NOTE: flushDocStores is ignored now (hardwired to true); this method is only here for backwards compatibility.
void forceMerge(int maxNumSegments)
This is a force merging policy to merge segments until there's <= maxNumSegments.
void forceMerge(int maxNumSegments, boolean doWait)
Just like forceMerge(int), except you can specify whether the call should block until all merging completes.
void forceMergeDeletes()
Forces merging of all segments that have deleted documents.
void forceMergeDeletes(boolean doWait)
Just like forceMergeDeletes(), except you can specify whether the call should be blocked until the operation completes.
Analyzer getAnalyzer()
Returns the analyzer used by this index.
IndexWriterConfig getConfig()
Returns the private IndexWriterConfig, cloned from the IndexWriterConfig passed to IndexWriter(Directory, IndexWriterConfig).
static PrintStream getDefaultInfoStream()
Returns the current default infoStream for newly instantiated IndexWriters.
static long getDefaultWriteLockTimeout()
Deprecated. Use IndexWriterConfig.getDefaultWriteLockTimeout() instead.
Directory getDirectory()
Returns the Directory used by this index.
PrintStream getInfoStream()
Returns the current infoStream in use by this writer.
int getMaxBufferedDeleteTerms()
Deprecated. Use IndexWriterConfig.getMaxBufferedDeleteTerms() instead.
int getMaxBufferedDocs()
Deprecated. Use IndexWriterConfig.getMaxBufferedDocs() instead.
int getMaxFieldLength()
Deprecated. Use LimitTokenCountAnalyzer to limit number of tokens.
int getMaxMergeDocs()
Deprecated. Use LogMergePolicy.getMaxMergeDocs() directly.
IndexWriter.IndexReaderWarmer getMergedSegmentWarmer()
Deprecated. Use IndexWriterConfig.getMergedSegmentWarmer() instead.
int getMergeFactor()
Deprecated. Use LogMergePolicy.getMergeFactor() directly.
MergePolicy getMergePolicy()
Deprecated. Use IndexWriterConfig.getMergePolicy() instead.
MergeScheduler getMergeScheduler()
Deprecated. Use IndexWriterConfig.getMergeScheduler() instead
Collection<SegmentInfo> getMergingSegments()
Expert: to be used by a MergePolicy to a void selecting merges for segments already being merged.
MergePolicy.OneMerge getNextMerge()
Expert: the MergeScheduler calls this method to retrieve the next merge requested by the MergePolicy.
PayloadProcessorProvider getPayloadProcessorProvider()
Returns the PayloadProcessorProvider that is used during segment merges to process payloads.
double getRAMBufferSizeMB()
Deprecated. Use IndexWriterConfig.getRAMBufferSizeMB() instead.
IndexReader getReader()
Deprecated. Use IndexReader.open(IndexWriter,boolean) instead.
IndexReader getReader(int termInfosIndexDivisor)
Deprecated. Use IndexReader.open(IndexWriter,boolean) instead. Furthermore, this method cannot guarantee the reader (and its sub-readers) will be opened with the termInfosIndexDivisor setting because some of them may already have been opened according to IndexWriterConfig.setReaderTermsIndexDivisor(int). You should set the requested termInfosIndexDivisor through IndexWriterConfig.setReaderTermsIndexDivisor(int) and use getReader().
int getReaderTermsIndexDivisor()
Deprecated. Use IndexWriterConfig.getReaderTermsIndexDivisor() instead.
Similarity getSimilarity()
Deprecated. Use IndexWriterConfig.getSimilarity() instead.
int getTermIndexInterval()
Deprecated. Use IndexWriterConfig.getTermIndexInterval().
boolean getUseCompoundFile()
Deprecated. Use LogMergePolicy.getUseCompoundFile().
long getWriteLockTimeout()
Deprecated. Use IndexWriterConfig.getWriteLockTimeout()
boolean hasDeletions()
static boolean isLocked(Directory directory)
Returns true if the index in the named directory is currently locked.
int maxDoc()
Returns total number of docs in this index, including docs not yet flushed (still in the RAM buffer), not counting deletions.
void maybeMerge()
Expert: Asks the mergePolicy whether any merges are necessary now and if so, runs the requested merges and then iterate (test again if merges are needed) until no more merges are returned by the mergePolicy.
void merge(MergePolicy.OneMerge merge)
Merges the indicated segments, replacing them in the stack with a single segment.
void message(String message)
Prints a message to the infoStream (if non-null), prefixed with the identifying information for this writer and the thread that's calling it.
int numDeletedDocs(SegmentInfo info)
Obtains the number of deleted docs for a pooled reader.
int numDocs()
Returns total number of docs in this index, including docs not yet flushed (still in the RAM buffer), and including deletions.
int numRamDocs()
Expert: Returns the number of documents currently buffered in RAM.
void optimize()
Deprecated.
void optimize(boolean doWait)
Deprecated.
void optimize(int maxNumSegments)
Deprecated.
void prepareCommit()
Expert: prepare for commit.
void prepareCommit(Map<String,String> commitUserData)
Expert: Prepare for commit, specifying commitUserData Map (String -> String).
long ramSizeInBytes()
Expert: Return the total size of all index files currently cached in memory.
void rollback()
Closes the IndexWriter without committing any changes that have occurred since the last commit (or since it was opened, if commit hasn't been called).
String segString()
String segString(Iterable<SegmentInfo> infos)
String segString(SegmentInfo info)
static void setDefaultInfoStream(PrintStream infoStream)
If non-null, this will be the default infoStream used by a newly instantiated IndexWriter.
static void setDefaultWriteLockTimeout(long writeLockTimeout)
Deprecated. Use IndexWriterConfig.setDefaultWriteLockTimeout(long) instead.
void setInfoStream(PrintStream infoStream)
If non-null, information about merges, deletes and a message when maxFieldLength is reached will be printed to this.
void setMaxBufferedDeleteTerms(int maxBufferedDeleteTerms)
Deprecated. Use IndexWriterConfig.setMaxBufferedDeleteTerms(int) instead.
void setMaxBufferedDocs(int maxBufferedDocs)
Deprecated. Use IndexWriterConfig.setMaxBufferedDocs(int) instead.
void setMaxFieldLength(int maxFieldLength)
Deprecated. Use LimitTokenCountAnalyzer instead. Observe the change in the behavior - the analyzer limits the number of tokens per token stream created, while this setting limits the total number of tokens to index. This matters only if you index many multi-valued fields though.
void setMaxMergeDocs(int maxMergeDocs)
Deprecated. Use LogMergePolicy.setMaxMergeDocs(int) directly.
void setMergedSegmentWarmer(IndexWriter.IndexReaderWarmer warmer)
Deprecated. Use IndexWriterConfig.setMergedSegmentWarmer( org.apache.lucene.index.IndexWriter.IndexReaderWarmer ) instead.
void setMergeFactor(int mergeFactor)
Deprecated. Use LogMergePolicy.setMergeFactor(int) directly.
void setMergePolicy(MergePolicy mp)
Deprecated. Use IndexWriterConfig.setMergePolicy(MergePolicy) instead.
void setMergeScheduler(MergeScheduler mergeScheduler)
Deprecated. Use IndexWriterConfig.setMergeScheduler(MergeScheduler) instead
void setPayloadProcessorProvider(PayloadProcessorProvider pcp)
Sets the PayloadProcessorProvider to use when merging payloads.
void setRAMBufferSizeMB(double mb)
Deprecated. Use IndexWriterConfig.setRAMBufferSizeMB(double) instead.
void setReaderTermsIndexDivisor(int divisor)
Deprecated. Use IndexWriterConfig.setReaderTermsIndexDivisor(int) instead.
void setSimilarity(Similarity similarity)
Deprecated. Use IndexWriterConfig.setSimilarity(Similarity) instead.
void setTermIndexInterval(int interval)
Deprecated. Use IndexWriterConfig.setTermIndexInterval(int).
void setUseCompoundFile(boolean value)
Deprecated. Use LogMergePolicy.setUseCompoundFile(boolean).
void setWriteLockTimeout(long writeLockTimeout)
Deprecated. Use IndexWriterConfig.setWriteLockTimeout(long) instead. 
static void unlock(Directory directory)
Forcibly unlocks the index in the named directory.
void updateDocument(Term term, Document doc)
Updates a document by first deleting the document(s) containing term and then adding the new document.
void updateDocument(Term term, Document doc, Analyzer analyzer)
Updates a document by first deleting the document(s) containing term and then adding the new document.
void updateDocuments(Term delTerm, Collection<Document> docs)
Atomically deletes documents matching the provided delTerm and adds a block of documents with sequentially assigned document IDs, such that an external reader will see all or none of the documents.
void updateDocuments(Term delTerm, Collection<Document> docs, Analyzer analyzer)
Atomically deletes documents matching the provided delTerm and adds a block of documents, analyzed using the provided analyzer, with sequentially assigned document IDs, such that an external reader will see all or none of the documents.
boolean verbose()
Returns true if verbosing is enabled (i.e., infoStream)
void waitForMerges()
Waits for any currently outstanding merges to finish.
This class inherits methods from the following classes −
java.lang.Object
 Print
 Add Notes
 Bookmark this page