Lucene - IndexWriter Class



This class acts as a core component which creates/updates indexes during indexing process.

Class declaration

Following is the declaration for org.apache.lucene.index.IndexWriter class −

public class IndexWriter
   extends Object
      implements Closeable, TwoPhaseCommit

Field

Following are the fields for the org.apache.lucene.index.IndexWriter class −

S.No. Field & Description
1

static final int MAX_DOCS

Hard limit on maximum number of documents that may be added to the index.

2

static final int MAX_POSITION

Maximum value of the token position in an indexed field.

3

static final int MAX_STORED_STRING_LENGTH

Maximum length string for a stored field.

4

static final int MAX_TERM_LENGTH

Absolute hard maximum length for a term, in bytes once encoded as UTF8.

5

static final int SOURCE

Key for the source of a segment in the diagnostics.

6

static final int SOURCE_ADDINDEXES_READERS

Source of a segment which results from a call to addIndexes(CodecReader...).

7

static final int SOURCE_FLUSH

Source of a segment which results from a flush.

8

static final int SOURCE_MERGE

Source of a segment which results from a merge of other segments.

9

static final int WRITE_LOCK_NAME

Name of the write lock in the index.

Class Constructors

Following table shows the class constructors for IndexWriter −

S.No. Constructor & Description
1

IndexWriter(Directory d, IndexWriterConfig conf)

Constructs a new IndexWriter per the settings given in conf.

S.No. Method & Description
1

long addDocument(Iterable<? extends IndexableField> doc)

Adds a document to this index.

2

long addDocuments(Iterable<? extends Iterable<? extends IndexableField>> 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.

3

long addIndexes(CodecReader... readers)

Merges the provided indexes into this index.

4

long addIndexes(Directory... dirs)

Adds all segments from an array of indexes into this index.

5

void addIndexesReaderMerge(MergePolicy.OneMerge merge)

Runs a single merge operation for addIndexes(CodecReader...).

6

void advanceSegmentInfosVersion(long newVersion)

If SegmentInfos.getVersion() is below newVersion then update it to this value.

7

void close()

Closes all open resources and releases the write lock.

8

final long commit()

Commits all pending changes (added and 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.

9

void decRefDeleter(SegmentInfos segmentInfos)

Record that the files referenced by this SegmentInfos are no longer in use.

10

long deleteAll()

Delete all documents in the index.

11

long deleteDocuments(Term... terms)

Deletes the document(s) containing any of the terms.

12

long deleteDocuments(Query... queries)

Deletes the document(s) matching any of the provided queries.

13

void deleteUnusedFiles()

Expert: remove any index files that are no longer used.

14

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).

15

protected void doBeforeFlush()

A hook for extending classes to execute operations before pending added and deleted documents are flushed to the Directory.

16

protected final void ensureOpen()

Used internally to throw an AlreadyClosedException if this IndexWriter has been closed (closed=true) or is in the process of closing (closing=true).

17

protected final void ensureOpen(boolean failIfClosing)

Used internally to throw an AlreadyClosedException if this IndexWriter has been closed or is in the process of closing.

18

final void flush()

Moves all in-memory segments to the Directory, but does not commit (fsync) them (call commit() for that).

19

final boolean flushNextBuffer()

Expert: Flushes the next pending writer per thread buffer if available or the largest active non-pending writer per thread buffer in the calling thread.

20

void forceMerge(int maxNumSegments)

Forces merge policy to merge segments until there are <= maxNumSegments.

21

void forceMerge(int maxNumSegments, boolean doWait)

Just like forceMerge(int), except you can specify whether the call should block until all merging completes.

22

void forceMergeDeletes()

Forces merging of all segments that have deleted documents.

23

void forceMergeDeletes(boolean doWait)

Just like forceMergeDeletes(), except you can specify whether the call should block until the operation completes.

24

Analyzer getAnalyzer()

Returns the analyzer used by this index.

25

LiveIndexWriterConfig getConfig()

Returns a LiveIndexWriterConfig, which can be used to query the IndexWriter current settings, as well as modify "live" ones.

26

Directory getDirectory()

Returns the Directory used by this index.

27

IndexWriter.DocStats getDocStats()

Returns accurate IndexWriter.DocStats for this writer.

28

Set<String> getFieldNames()

Return an unmodifiable set of all field names as visible from this IndexWriter, across all segments of the index.

29

final long getFlushingBytes()

Returns the number of bytes currently being flushed.

30

InfoStream getInfoStream()

Returns the info stream that can be used to log messages.

31

final Iterable<Map.Entry<String,String>> getLiveCommitData()

Returns the commit user data iterable previously set with setLiveCommitData(Iterable), or null if nothing has been set yet.

32

long getMaxCompletedSequenceNumber()

Returns the highest sequence number across all completed operations, or 0 if no operations have finished yet.

33

Set<SegmentCommitInfo> getMergingSegments()

Expert: to be used by a MergePolicy to avoid selecting merges for segments already being merged.

34

long getPendingNumDocs()

Returns the number of documents in the index including documents are being added (i.e., reserved).

35

Throwable getTragicException()

If this IndexWriter was closed as a side-effect of a tragic exception.

36

boolean hasDeletions()

Returns true if this index has deletions (including buffered deletions).

37

boolean hasPendingMerges()

Expert: returns true if there are merges waiting to be scheduled.

38

final boolean hasUncommittedChanges()

Returns true if there may be changes that have not been committed.

39

void incRefDeleter(SegmentInfos segmentInfos)

Record that the files referenced by this SegmentInfos are still in use.

40

protected boolean isEnableTestPoints()

Tests should override this to enable test points.

41

boolean isOpen()

Returns true if this IndexWriter is still open.

42

final 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.

43

protected void merge(MergePolicy.OneMerge merge)

Merges the indicated segments, replacing them in the stack with a single segment.

44

protected void mergeSuccess(MergePolicy.OneMerge merge)

Hook that's called when the specified merge is complete.

45

int numDeletedDocs(SegmentCommitInfo info)

Obtain the number of deleted docs for a pooled reader.

46

final int numDeletesToMerge(SegmentCommitInfo info)

Returns the number of deletes a merge would claim back if the given segment is merged.

47

final int numRamDocs()

Expert: Return the number of documents currently buffered in RAM.

48

void onTragicEvent(Throwable tragedy, String location)

This method should be called on a tragic event ie.

49

final long prepareCommit()

Expert: prepare for commit.

50

final long ramBytesUsed()

Return the memory usage of this object in bytes.

51

void rollback()

Close the IndexWriter without committing any changes that have occurred since the last commit (or since it was opened, if commit hasn't been called).

52

final void setLiveCommitData(Iterable<Map.Entry<String,String>> commitUserData)

Sets the iterator to provide the commit user data map at commit time.

53

final void setLiveCommitData(Iterable<Map.Entry<String,String>> commitUserData, boolean doIncrementVersion)

Sets the commit user data iterator, controlling whether to advance the SegmentInfos.getVersion().

54

long softUpdateDocument(Term term, Iterable<? extends IndexableField> doc, Field... softDeletes)

Expert: Updates a document by first updating the document(s) containing term with the given doc-values fields and then adding the new document.

55

long softUpdateDocuments(Term term, Iterable<? extends Iterable<? extends IndexableField>> docs, Field... softDeletes)

Expert: Atomically updates documents matching the provided term with the given doc-values fields and adds a block of documents with sequentially assigned document IDs, such that an external reader will see all or none of the documents.

56

long tryDeleteDocument(IndexReader readerIn, int docID)

Expert: attempts to delete by document ID, as long as the provided reader is a near-real-time reader (from DirectoryReader.open(IndexWriter)).

57

long tryUpdateDocValue(IndexReader readerIn, int docID, Field... fields)

Expert: attempts to update doc values by document ID, as long as the provided reader is a near-real-time reader (from DirectoryReader.open(IndexWriter)).

58

long updateBinaryDocValue(Term term, String field, BytesRef value)

Updates a document's BinaryDocValues for field to the given value.

59

long updateDocument(Term term, Iterable<? extends IndexableField> doc)

Updates a document by first deleting the document(s) containing term and then adding the new document.

60

long updateDocuments(Term delTerm, Iterable<? extends Iterable<? extends IndexableField>> 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.

61

long updateDocuments(Query delQuery, Iterable<? extends Iterable<? extends IndexableField>> docs)

Similar to updateDocuments(Term, Iterable), but take a query instead of a term to identify the documents to be updated.

62

long updateDocValues(Term term, Field... updates)

Updates documents' DocValues fields to the given values.

63

long updateNumericDocValue(Term term, String field, long value)

Updates a document's NumericDocValues for field to the given value.

Methods Inherited

This class inherits methods from the following classes −

  • java.lang.Object
lucene_indexing_classes.htm
Advertisements