org.apache.lucene.index

## Class RandomIndexWriter

- java.lang.Object
- - org.apache.lucene.index.RandomIndexWriter

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public class RandomIndexWriter
      extends Object
      implements Closeable

  Silly class that randomizes the indexing experience. EG it may swap in a different merge policy/scheduler; may commit periodically; may or may not forceMerge in the end, may flush by doc count instead of RAM, etc.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `RandomIndexWriter.TestPoint` Simple interface that is executed for each `TP` `InfoStream` component message.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `IndexWriter` | `w`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RandomIndexWriter(Random r, Directory dir)` create a RandomIndexWriter with a random config: Uses MockAnalyzer
    `RandomIndexWriter(Random r, Directory dir, Analyzer a)` create a RandomIndexWriter with a random config
    `RandomIndexWriter(Random r, Directory dir, IndexWriterConfig c)` create a RandomIndexWriter with the provided config
    `RandomIndexWriter(Random r, Directory dir, IndexWriterConfig c, boolean useSoftDeletes)` create a RandomIndexWriter with the provided config

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<T extends IndexableField> long` | `addDocument(Iterable<T> doc)` Adds a Document.
    `long` | `addDocuments(Iterable<? extends Iterable<? extends IndexableField>> docs)`
    `long` | `addIndexes(CodecReader... readers)`
    `long` | `addIndexes(Directory... dirs)`
    `void` | `close()` Close this writer.
    `long` | `commit()`
    `long` | `commit(boolean flushConcurrently)`
    `long` | `deleteAll()`
    `long` | `deleteDocuments(Query q)`
    `long` | `deleteDocuments(Term term)`
    `void` | `flush()` Writes all in-memory segments to the `Directory`.
    `void` | `forceMerge(int maxSegmentCount)` Forces a forceMerge.
    `void` | `forceMergeDeletes()`
    `void` | `forceMergeDeletes(boolean doWait)`
    `IndexWriter.DocStats` | `getDocStats()`
    `DirectoryReader` | `getReader()`
    `DirectoryReader` | `getReader(boolean applyDeletions, boolean writeAllDeletes)`
    `static IndexWriter` | `mockIndexWriter(Directory dir, IndexWriterConfig conf, Random r)` Returns an indexwriter that randomly mixes up thread scheduling (by yielding at test points)
    `static IndexWriter` | `mockIndexWriter(Random r, Directory dir, IndexWriterConfig conf, RandomIndexWriter.TestPoint testPoint)` Returns an indexwriter that enables the specified test point
    `void` | `setDoRandomForceMerge(boolean v)`
    `void` | `setDoRandomForceMergeAssert(boolean v)`
    `long` | `updateBinaryDocValue(Term term, String field, BytesRef value)`
    `<T extends IndexableField> long` | `updateDocument(Term t, Iterable<T> doc)` Updates a document.
    `long` | `updateDocuments(Term delTerm, Iterable<? extends Iterable<? extends IndexableField>> docs)`
    `long` | `updateDocValues(Term term, Field... updates)`
    `long` | `updateNumericDocValue(Term term, String field, Long value)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### w

          public final IndexWriter w

  - ### Constructor Detail

    - #### RandomIndexWriter

          public RandomIndexWriter(Random r,
                                   Directory dir)
                            throws IOException

      create a RandomIndexWriter with a random config: Uses MockAnalyzer
      Throws:
        `IOException`

    - #### RandomIndexWriter

          public RandomIndexWriter(Random r,
                                   Directory dir,
                                   Analyzer a)
                            throws IOException

      create a RandomIndexWriter with a random config
      Throws:
        `IOException`

    - #### RandomIndexWriter

          public RandomIndexWriter(Random r,
                                   Directory dir,
                                   IndexWriterConfig c)
                            throws IOException

      create a RandomIndexWriter with the provided config
      Throws:
        `IOException`

    - #### RandomIndexWriter

          public RandomIndexWriter(Random r,
                                   Directory dir,
                                   IndexWriterConfig c,
                                   boolean useSoftDeletes)
                            throws IOException

      create a RandomIndexWriter with the provided config
      Throws:
        `IOException`

  - ### Method Detail

    - #### mockIndexWriter

          public static IndexWriter mockIndexWriter(Directory dir,
                                                    IndexWriterConfig conf,
                                                    Random r)
                                             throws IOException

      Returns an indexwriter that randomly mixes up thread scheduling (by yielding at test points)
      Throws:
        `IOException`

    - #### mockIndexWriter

          public static IndexWriter mockIndexWriter(Random r,
                                                    Directory dir,
                                                    IndexWriterConfig conf,
                                                    RandomIndexWriter.TestPoint testPoint)
                                             throws IOException

      Returns an indexwriter that enables the specified test point
      Throws:
        `IOException`

    - #### addDocument

          public <T extends IndexableField> long addDocument(Iterable<T> doc)
                                                      throws IOException

      Adds a Document.
      Throws:
        `IOException`
      See Also:
        `IndexWriter.addDocument(Iterable)`

    - #### addDocuments

          public long addDocuments(Iterable<? extends Iterable<? extends IndexableField>> docs)
                            throws IOException

      Throws:
        `IOException`

    - #### updateDocuments

          public long updateDocuments(Term delTerm,
                                      Iterable<? extends Iterable<? extends IndexableField>> docs)
                               throws IOException

      Throws:
        `IOException`

    - #### updateDocument

          public <T extends IndexableField> long updateDocument(Term t,
                                                                Iterable<T> doc)
                                                         throws IOException

      Updates a document.
      Throws:
        `IOException`
      See Also:
        `IndexWriter.updateDocument(Term, Iterable)`

    - #### addIndexes

          public long addIndexes(Directory... dirs)
                          throws IOException

      Throws:
        `IOException`

    - #### addIndexes

          public long addIndexes(CodecReader... readers)
                          throws IOException

      Throws:
        `IOException`

    - #### updateNumericDocValue

          public long updateNumericDocValue(Term term,
                                            String field,
                                            Long value)
                                     throws IOException

      Throws:
        `IOException`

    - #### updateBinaryDocValue

          public long updateBinaryDocValue(Term term,
                                           String field,
                                           BytesRef value)
                                    throws IOException

      Throws:
        `IOException`

    - #### updateDocValues

          public long updateDocValues(Term term,
                                      Field... updates)
                               throws IOException

      Throws:
        `IOException`

    - #### deleteDocuments

          public long deleteDocuments(Term term)
                               throws IOException

      Throws:
        `IOException`

    - #### deleteDocuments

          public long deleteDocuments(Query q)
                               throws IOException

      Throws:
        `IOException`

    - #### commit

          public long commit()
                      throws IOException

      Throws:
        `IOException`

    - #### commit

          public long commit(boolean flushConcurrently)
                      throws IOException

      Throws:
        `IOException`

    - #### getDocStats

          public IndexWriter.DocStats getDocStats()

    - #### deleteAll

          public long deleteAll()
                         throws IOException

      Throws:
        `IOException`

    - #### getReader

          public DirectoryReader getReader()
                                    throws IOException

      Throws:
        `IOException`

    - #### forceMergeDeletes

          public void forceMergeDeletes(boolean doWait)
                                 throws IOException

      Throws:
        `IOException`

    - #### forceMergeDeletes

          public void forceMergeDeletes()
                                 throws IOException

      Throws:
        `IOException`

    - #### setDoRandomForceMerge

          public void setDoRandomForceMerge(boolean v)

    - #### setDoRandomForceMergeAssert

          public void setDoRandomForceMergeAssert(boolean v)

    - #### getReader

          public DirectoryReader getReader(boolean applyDeletions,
                                           boolean writeAllDeletes)
                                    throws IOException

      Throws:
        `IOException`

    - #### close

          public void close()
                     throws IOException

      Close this writer.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException`
      See Also:
        `IndexWriter.close()`

    - #### forceMerge

          public void forceMerge(int maxSegmentCount)
                          throws IOException

      Forces a forceMerge.

      NOTE: this should be avoided in tests unless absolutely necessary, as it will result in less test coverage.

      Throws:
        `IOException`
      See Also:
        `IndexWriter.forceMerge(int)`

    - #### flush

          public final void flush()
                           throws IOException

      Writes all in-memory segments to the `Directory`.
      Throws:
        `IOException`

Copyright © 2000-2024 Apache Software Foundation. All Rights Reserved.