Java Code Examples for org.apache.lucene.index.IndexWriterConfig.OpenMode#CREATE_OR_APPEND

The following examples show how to use org.apache.lucene.index.IndexWriterConfig.OpenMode#CREATE_OR_APPEND . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommit() throws Exception {
  // Verifies that nothing is committed to the underlying Directory, if
  // commit() wasn't called.
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  assertFalse(DirectoryReader.indexExists(dir));
  ltw.commit(); // first commit, so that an index will be created
  ltw.addCategory(new FacetLabel("a"));
  
  IndexReader r = DirectoryReader.open(dir);
  assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
  r.close();
  ltw.close();
  dir.close();
}
 
Example 2
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBackwardsCompatibility() throws Exception {
  // tests that if the taxonomy index doesn't have the INDEX_EPOCH
  // property (supports pre-3.6 indexes), all still works.
  Directory dir = newDirectory();
  
  // create an empty index first, so that DirTaxoWriter initializes indexEpoch to 1.
  new IndexWriter(dir, new IndexWriterConfig(null)).close();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  taxoWriter.close();
  
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
  assertEquals(1, Integer.parseInt(taxoReader.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_EPOCH)));
  assertNull(TaxonomyReader.openIfChanged(taxoReader));
  taxoReader.close();
  
  dir.close();
}
 
Example 3
Source File: LiveIndexWriterConfig.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
LiveIndexWriterConfig(Analyzer analyzer) {
  this.analyzer = analyzer;
  ramBufferSizeMB = IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB;
  maxBufferedDocs = IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS;
  mergedSegmentWarmer = null;
  delPolicy = new KeepOnlyLastCommitDeletionPolicy();
  commit = null;
  useCompoundFile = IndexWriterConfig.DEFAULT_USE_COMPOUND_FILE_SYSTEM;
  openMode = OpenMode.CREATE_OR_APPEND;
  similarity = IndexSearcher.getDefaultSimilarity();
  mergeScheduler = new ConcurrentMergeScheduler();
  indexingChain = DocumentsWriterPerThread.defaultIndexingChain;
  codec = Codec.getDefault();
  if (codec == null) {
    throw new NullPointerException();
  }
  infoStream = InfoStream.getDefault();
  mergePolicy = new TieredMergePolicy();
  flushPolicy = new FlushByRamOrCountsPolicy();
  readerPooling = IndexWriterConfig.DEFAULT_READER_POOLING;
  perThreadHardLimitMB = IndexWriterConfig.DEFAULT_RAM_PER_THREAD_HARD_LIMIT_MB;
  maxCommitMergeWaitMillis = IndexWriterConfig.DEFAULT_MAX_COMMIT_MERGE_WAIT_MILLIS;
}
 
Example 4
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecreateAndRefresh() throws Exception {
  // DirTaxoWriter lost the INDEX_EPOCH property if it was opened in
  // CREATE_OR_APPEND (or commit(userData) called twice), which could lead to
  // DirTaxoReader succeeding to refresh().
  try (Directory dir = newDirectory()) {

    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
    touchTaxo(taxoWriter, new FacetLabel("a"));

    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);

    touchTaxo(taxoWriter, new FacetLabel("b"));

    TaxonomyReader newtr = TaxonomyReader.openIfChanged(taxoReader);
    taxoReader.close();
    taxoReader = newtr;
    assertEquals(1, Integer.parseInt(taxoReader.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_EPOCH)));

    // now recreate the taxonomy, and check that the epoch is preserved after opening DirTW again.
    taxoWriter.close();

    taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE, NO_OP_CACHE);
    touchTaxo(taxoWriter, new FacetLabel("c"));
    taxoWriter.close();

    taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
    touchTaxo(taxoWriter, new FacetLabel("d"));
    taxoWriter.close();

    newtr = TaxonomyReader.openIfChanged(taxoReader);
    taxoReader.close();
    taxoReader = newtr;
    assertEquals(2, Integer.parseInt(taxoReader.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_EPOCH)));
    taxoReader.close();
  }
}
 
Example 5
Source File: DirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Create this with {@code OpenMode.CREATE_OR_APPEND}. */
public DirectoryTaxonomyWriter(Directory d) throws IOException {
  this(d, OpenMode.CREATE_OR_APPEND);
}
 
Example 6
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testCommitUserData() throws Exception {
  // Verifies taxonomy commit data
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  assertTrue(taxoWriter.getCache() == NO_OP_CACHE);
  taxoWriter.addCategory(new FacetLabel("a"));
  taxoWriter.addCategory(new FacetLabel("b"));
  Map<String, String> userCommitData = new HashMap<>();
  userCommitData.put("testing", "1 2 3");
  taxoWriter.setLiveCommitData(userCommitData.entrySet());
  taxoWriter.close();
  DirectoryReader r = DirectoryReader.open(dir);
  assertEquals("2 categories plus root should have been committed to the underlying directory", 3, r.numDocs());
  Map <String, String> readUserCommitData = r.getIndexCommit().getUserData();
  assertTrue("wrong value extracted from commit data", 
      "1 2 3".equals(readUserCommitData.get("testing")));
  assertNotNull(DirectoryTaxonomyWriter.INDEX_EPOCH + " not found in commitData", readUserCommitData.get(DirectoryTaxonomyWriter.INDEX_EPOCH));
  r.close();
  
  // open DirTaxoWriter again and commit, INDEX_EPOCH should still exist
  // in the commit data, otherwise DirTaxoReader.refresh() might not detect
  // that the taxonomy index has been recreated.
  taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  taxoWriter.addCategory(new FacetLabel("c")); // add a category so that commit will happen
  taxoWriter.setLiveCommitData(new HashMap<String, String>(){{
    put("just", "data");
  }}.entrySet());
  taxoWriter.commit();
  
  // verify taxoWriter.getCommitData()
  Map<String,String> data = new HashMap<>();
  Iterable<Map.Entry<String,String>> iter = taxoWriter.getLiveCommitData();
  if (iter != null) {
    for(Map.Entry<String,String> ent : iter) {
      data.put(ent.getKey(), ent.getValue());
    }
  }
  
  assertNotNull(DirectoryTaxonomyWriter.INDEX_EPOCH
      + " not found in taoxWriter.commitData", data.get(DirectoryTaxonomyWriter.INDEX_EPOCH));
  taxoWriter.close();
  
  r = DirectoryReader.open(dir);
  readUserCommitData = r.getIndexCommit().getUserData();
  assertNotNull(DirectoryTaxonomyWriter.INDEX_EPOCH + " not found in commitData", readUserCommitData.get(DirectoryTaxonomyWriter.INDEX_EPOCH));
  r.close();
  
  dir.close();
}
 
Example 7
Source File: PersistentSnapshotDeletionPolicy.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * {@link PersistentSnapshotDeletionPolicy} wraps another
 * {@link IndexDeletionPolicy} to enable flexible
 * snapshotting, passing {@link OpenMode#CREATE_OR_APPEND}
 * by default.
 * 
 * @param primary
 *          the {@link IndexDeletionPolicy} that is used on non-snapshotted
 *          commits. Snapshotted commits, by definition, are not deleted until
 *          explicitly released via {@link #release}.
 * @param dir
 *          the {@link Directory} which will be used to persist the snapshots
 *          information.
 */
public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary,
    Directory dir) throws IOException {
  this(primary, dir, OpenMode.CREATE_OR_APPEND);
}
 
Example 8
Source File: SolrSnapshotMetaDataManager.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param dir The directory where the snapshot meta-data should be stored. Enables updating
 *            the existing meta-data.
 * @throws IOException in case of errors.
 */
public SolrSnapshotMetaDataManager(SolrCore solrCore, Directory dir) throws IOException {
  this(solrCore, dir, OpenMode.CREATE_OR_APPEND);
}